Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


Search this Thread
Old 07-02-2004, 06:19 AM   #1
LQ Newbie
Registered: May 2004
Posts: 3

Rep: Reputation: 0
foreach loops

I have two files that I need to combine, as the combined tables need to be read into a programme.

1st table is shots (046)

2nd Table (046scene_config)

The second table is the record-in value for each shot on a timeline.

#! /bin/tcsh -f
foreach i ( `cat 046` )
foreach j ( `cat /sc046_config | awk 'BEGIN { FS = "[,]" } ; { print $4 } '`)
echo $i $j

The results are:
comp/sc046sh001_comp.%04d.dpx 0
comp/sc046sh001_comp.%04d.dpx 121
comp/sc046sh001_comp.%04d.dpx 232
comp/sc046sh001_comp.%04d.dpx 300
comp/sc046sh002_comp.%04d.dpx 0
comp/sc046sh002_comp.%04d.dpx 121
comp/sc046sh002_comp.%04d.dpx 232
comp/sc046sh002_comp.%04d.dpx 300
comp/sc046sh003_comp.%04d.dpx 0
comp/sc046sh003_comp.%04d.dpx 121
comp/sc046sh003_comp.%04d.dpx 232
comp/sc046sh003_comp.%04d.dpx 300

What I am trying to achieve is:
comp/sc046sh001_comp.%04d.dpx 0
comp/sc046sh002_comp.%04d.dpx 121
comp/sc046sh003_comp.%04d.dpx 232
comp/sc046sh004_comp.%04d.dpx 300

So the first line of the first table relates to the first line of the second table, and this continues down until the end. How do I stop it looping around, so that each line of the first table does every line of the second table.

Any help much appreciated.


Old 07-02-2004, 08:33 AM   #2
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
There is a command "paste" that will do this for you without needing foreach loops.


awk -F\, '{print $4}' sc046_config > TEMP
paste 046 TEMP > NEWFILE


paste 046 `awk -F\, '{print $4}' sc046_config` > NEWFILE


Last edited by jkobrien; 07-02-2004 at 08:37 AM.
Old 07-02-2004, 11:49 AM   #3
Registered: Dec 2003
Location: Little Rock, AR
Distribution: Fedora Core 2, AIX, HP-UX, Solaris, Whitebox
Posts: 193

Rep: Reputation: 31
Another way, which might be a bit longer...

First figure out how many lines are in the first file (add one to this for the loop later):

let howLong=`cat (046)|wc -l`+1

Then walk through the files joining the lines together
let lineNum=1
while ($lineNum -lt $howLong)
echo `sed -n "$lineNum"p 046` `sed -n "$lineNum"p sc046_config` >> NEWFILE
let lineNum=$lineNum+1


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use foreach on filenames with spaces? BrianK Linux - General 3 08-09-2005 12:15 PM
PHP posting value to session using foreach Erhnam Programming 15 02-16-2005 08:31 AM
[php] Posting correct value when using foreach Erhnam Programming 1 02-10-2005 03:12 PM
Perl: Where am I in a foreach loop? jrtayloriv Programming 3 01-30-2005 10:43 PM
perl:foreach katana Programming 3 07-24-2001 01:05 AM

All times are GMT -5. The time now is 11:28 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration