losing white space IFS

I want to read a file of COBOL source and detect the occurrence or certain strings. If they exist I then want to include an additional record in the output. The problem being that shell read destroys the format by removing extra white space if I do and expands "*" in my input

OK, I see what you are after.

First: this kind of thing is much easier and more fun to do in Perl- see Why I love Perl.

However, the shell doesn't really "destroy" anything; it's just doing what it should.

Try this:

while :
 do read a || exit
 echo $a
done

# $a will contain "123456 MOVE A TO B"
# when the original is "123456     MOVE    A    TO    B"
# but if you use this script:
IFS=""
while :
 do read a || exit
 echo $a
done

# or this:
while :
 do read a || exit
 echo "$a"
done

# the white space will be preserved

That takes care of the white spaces.

The "*" problem would also be fixed by double quoting:

echo "$a"
 

Try that with your input that has "*" in it.

So why did I mention IFS? Because it gives you much more power.

The IFS controls the "Input Field Separator"

Try this in a script (don't mess with IFS at the command line- you'll get all screwed up!):

IFS=":"
while read line
do
set $line
echo $@
done 
 

Feed that "One:two:three" and get back ""One two three".

See $IFS in Advanced Bash-Scripting Guide: 9.1. Internal Variables for more.

How can I transfer SCO accounts (passwd information) to Linux? uses IFS to parse the /etc/passwd file.

See Shell Programming: IFS variable for interesting comments on this subject.

But again: spend just a little time with Perl and you'll never look back.



Got something to add? Send me email.





(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> losing white space IFS ––>Re: white space in read


2 comments



Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Tony Lawrence







Mon Jul 22 13:53:52 2013: 12241   vinicius

gravatar


are you sure about the last example? It doesn't seem to work in bash. As you're reading only one variable, it doesn't matter what is the IFS separator, correct?

One trick I use to change the IFS only for the read command is to run it in the same 'read', like:
IFS=":" read var1 var2 var3 ...

Regards.



Mon Jul 22 14:08:26 2013: 12242   TonyLawrence

gravatar


ooops.

I meant

 
while read line
do
set $line
echo $@
done


I'll fix that, thanks




------------------------
Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us





An incompetent attorney can delay a trial for years or months. A competent attorney can delay one even longer. (Evelle J. Younger)

Why bother with subroutines when you can type fast? (Vaughn Rokosz)








This post tagged: