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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extract lines one by one 2

Status
Not open for further replies.

kasparov

Programmer
Feb 13, 2002
203
GB
I have a file with 3 fields separated by spaces & I want to extract each line one by one & then perform some processing on each one. I assumed I could do this:

for line in `cat my_file`
do
do my processing here ...
done

but this extracts each field (so it takes 3 iterations of the for loop to do one line). I'm sure I'm missing something obvious but I just can't think what.

Can anyone help?
 
hi,

Try this ,

cat my_file | while read A B C
do
process your stuff here using $A $B $C
print $A $B $C
done

HTH
 

please do not reinvent the shell, use its features !!

#!/bin/sh
IFS="\n"
for aaa in `cat filename`
do echo $aaa
done
 
Hi DSMARWAY........
Helped me too - but what if the separator isn't a space ?
;-) Dickie Bird
db@dickiebird.freeserve.co.uk
 
IFS can be set to every chars NOT in the range [A-z0-9_]
 
Thanks jamisar - I knew there was a way to do it ...!
 
I would use DSMARWAY's method.

If you want the whole line in a single variable:
Code:
cat ... | while read line; do ...

For a discution about IFS and while read vs for in `cat`, look thread822-302325.

 
ooops - darn copy&paste:

#!/bin/sh
IFS="\n" while read aaa
do
echo $aaa
done < filename

vlad vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
man, am I bad.... sorry

#!/bin/sh
while IFS=&quot;\n&quot; read aaa
do
echo $aaa
done < filename
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Vlad:

You get a star for pointing out UUOC, and promoting good coding practices.

As a maintenance programmer I won't &quot;fix&quot;a UUOC for fear of breaking something else, but I don't believe in creating new ones.

Thanks!

Ed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top