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.
More Articles by Tony Lawrence © 2013-08-20 Tony Lawrence
Great minds discuss ideas; average minds discuss events; small minds discuss people. (Eleanor Roosevelt)
Mon Jul 22 13:53:52 2013: 12241 vinicius
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
ooops.
I meant
I'll fix that, thanks
------------------------
Printer Friendly Version
Losing white space IFS Copyright © July 2013 Tony Lawrence
Have you tried Searching this site?
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
Printer Friendly Version