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.
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:
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
Increase ad revenue 50-250% with Ezoic