Help me understand this!

Brian K. White brian at aljex.com
Wed Jul 21 12:02:08 PDT 2004


Lerebours, Jose wrote:
>> From filePro process, I am using
>
> x=open(file,"rwtc"), y=write(x,string) and z=close(x)
>
> Now, string holds value from two fields
>
> field1(10,*) and field2(13,*)
>
> content in field1 is mostly not longer than 8 char
>
> So, the written file ends-up looking like so
>
> 0123456789012345678901234567890
> FIELD1LN1  FIELD2LINE1
> FIELD1LN2  FIELD2LINE2
> FIELD1LN3  FIELD2LINE3
>
> etc, etc, etc ...
>
> I then use shell script to run a for/next loop
>
> for X in `cat filename`
> do
>
> echo "$X"
>
> done
>
> The displayed text is as follows
>
> FIELD1LN1
> FIELD1LINE1
> FIELD1LN2
> FIELD2LINE2
> FIELD1LN3
> FIELD2LINE3
>
> I am expecting to retain both fields side by side
> in $X.  If I vi the file it looks fine.  If I cat
> the file it looks fine ...
>
> Why is this?  How can I get what I am looking for?
>
> Thank you all in advance!


the problem is the way the "for" command works in the shell.
that `cat filename` produces:

word1 word2
word3 word4

but the for command doesn't distiguish between different types of word
seperators (whitespaces)

so the for command sees:
word1 word2 word3 word4
the same way it sees:
word1
word2
word3
word4

and it perfroms the action once for each "word"
and since your action produces "word\n" that is what is displayed and it
always results in:
word1
word2
word3
word4

Normally, careful attention to quoting gets you around such things, but in
this case you'll have to use a different tool to read the data if you want
to step through by line instead of by word.

if you want to run a procedure on each line of the file, and the lines
contain spaces, and you don't want to get kludgy by running the data through
tr or sed to change the spaces to _ or something, then I'd use awk

awk '{system("myscript "$0)}' file

if "myscript looked like this:
#!/bin/ksh
echo $@

then the end result would look the same as just catting the file

You might be able to avoid the external utility (awk/perl) and pull off some
trick involving telling the shell that spaces are not word seperators via
command line options or built-in variable setting, but I'd avoid things like
that.

Brian K. White  --  brian at aljex.com  --  http://www.aljex.com/bkw/
+++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++.
filePro BBx  Linux SCO  Prosper/FACTS AutoCAD  #callahans Satriani






More information about the Filepro-list mailing list