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

Using while loop on file with spaces in some fields

Status
Not open for further replies.

johngiggs

Technical User
Oct 30, 2002
492
US
How can I use a while loop to read a file that has spaces.

Ex. input.file looks like this:

Field 1 Field 2 Field 3

cat input.file | while read field1 field2 field3
do
...
done

The problem I'm having is that the while loop is breaking up the fields into 6 fields instead of 3.

Any help would be greatly appreciated.

Thanks,

John
 
So the fields themselves have spaces in them? Is each field always two words? Are the fields separated by tabs rather than spaces (which would allow you to differentiate between field separators and spaces)?

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Annihilannic,

Since I was using bash, I used the IFS and OFS variables.

OLDIFS=$IFS
IFS=";"

while ...
do
...
done < /my/input/file
IFS=$OLDIFS
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top