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

Can I use AWK to read a file line by line and feed to a program?

Status
Not open for further replies.

QAGuy

MIS
Oct 16, 2000
5
US
I need to read a file line by line and feed the input into a program. All of this is happening within a shell script. The file has two fields, and I need to assign the values to variables which will then be passed to another program. I'm using "for file in file_name do" to go through the file. Thanks [sig][/sig]
 
Hi, haven't tried this and my memory may let me down ... but can't you do something like this below. I'll assume the fields are separated by space, but you can specify anything in the -d switch in cut.

[tt]
for fline in `cat myfilename`
do
VAR1=`cut -d" " -f1 $fline`
VAR2=`cut -d" " -f2 $fline`
#run your program
done
[/tt]

It's Unix, so no doubt there are a hundred other ways too!

Greg. [sig][/sig]
 
This also works when you have more then one field per line.

while read x
do
set $x
echo $# ## How many fields
echo $3 ## Print the third field
done < inputfile


Regards Gregor. [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top