Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I process multi-column data in ksh script

Status
Not open for further replies.

Tison

Programmer
May 12, 1999
216
0
0
CH
I have a multi-column data file that I want to read in and do some task on each line.
FIELD1` is a file number
FIELD2 is a file name
FIELD3 is a date
For each record in the file, copy the file $FIELD1 from a directory into another directory but then append $FIELD2 to the filename. ie.
for record in myfile
do
spilt record into FIELD1,FIELD2,FIELD3
cp /tmpdir/$FIELD1 to /otherdir/$FIELD3.$FIELD2.$FIELD1
done

The problem is that the "for" loop treats each field as one line.
My data file is ;
12345 JOBNAME1 26/03/2001
24314 JOBNAMEX 26/04/2001
12345 JOBNAME4 27/03/2001


 
Hi Tison,
You could do this in multiple ways. Here are 3 ways you could do this.

1.
cat filename | while read FIELD1 FIELD2 FILED3
do
cp /tmpdir/$FIELD1 /otherdir/$FIELD3.$FIELD2.$FIELD1
done

2.
exec 3< filename
while read -u3 FIELD1 FIELD2 FIELD3
do
cp /tmpdir/$FIELD1 /otherdir/$FIELD3.$FIELD2.$FIELD1
done


3.
while x=`line`
do
set $x
FIELD1=$1
FIELD2=$2
FIELD3=$3
cp /tmpdir/$FIELD1 /otherdir/$FIELD3.$FIELD2.$FIELD1
done < filename

HTH,

regards
Nana Srinivasan

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top