for i in `cat file` ... separator=\n instead of whitespace

for i in `cat file` ... separator=\n instead of whitespace

Post by Georges Kessel » Fri, 12 Nov 1993 01:37:35



Hi
this works:
$for i in 'a b c' 'd e f'
Quote:>do
>  echo $i
>done

a b c
d e f

This does not:
$cat file
a b c
d e f
$for i in `cat file`

Quote:>do
> echo $i
>done

a
b
c
d
e
f

How do I give the for the strings "a b c" "d e f" ? I tried adding " with awk
and tricks with eval, but it does not work. Is there a solution?
I would prefer not to reread the entire file for each line ...

Georges.
--
Georges Kesseler


IRC: Gunstick

 
 
 

for i in `cat file` ... separator=\n instead of whitespace

Post by Arne Henrik Ju » Fri, 12 Nov 1993 03:10:04




 >  Newsgroups: comp.unix.shell
 >  Date: 10 Nov 1993 16:37:35 GMT
 >  Organization: cetrel
 >
 >  Hi
 >  this works:
 >  $for i in 'a b c' 'd e f'
 >  >do
 >  >  echo $i
 >  >done
 >  a b c
 >  d e f
 >
 >  This does not:
 >  $cat file
 >  a b c
 >  d e f
 >  $for i in `cat file`
 >  >do
 >  > echo $i
 >  >done
 >  a
 >  b
 >  c
 >  d
 >  e
 >  f
 >
 >  How do I give the for the strings "a b c" "d e f" ? I tried adding " with awk
 >  and tricks with eval, but it does not work. Is there a solution?
 >  I would prefer not to reread the entire file for each line ...

Use:

$ while read i
  do
    echo $i
  done < file

The while-read loop is a very important part of sh programming,
IMNSHO.

 - Arne H. Juul

 
 
 

for i in `cat file` ... separator=\n instead of whitespace

Post by David W. Tamk » Sat, 13 Nov 1993 05:53:04




Well, you can check what he wrote.

Arne Juul has already suggested this (which also avoids the call to cat):

        while read i
        do echo "$i"
        done < file

Though I prefer that solution and would have suggested it myself had it not
already been posted, this works too (under sh) with the original syntax:

        OLDIFS=$IFS
        IFS="
        "
        for i in `cat file`
        do echo $i
        done
        IFS=$OLDIFS

But the while-read answer is much better.

David W. Tamkin  P. O. Box 3284  Skokie, Illinois  60076-6284

 
 
 

for i in `cat file` ... separator=\n instead of whitespace

Post by David Pledg » Sat, 13 Nov 1993 07:05:30



|> Hi
|> this works:
|> $for i in 'a b c' 'd e f'
|> >do
|> >  echo $i
|> >done
|> a b c
|> d e f
|>
|> This does not:
|> $cat file
|> a b c
|> d e f
|> $for i in `cat file`
|> >do
|> > echo $i
|> >done
|> a
|> b
|> c
|> d
|> e
|> f
|>
|> How do I give the for the strings "a b c" "d e f" ? I tried adding " with awk
|> and tricks with eval, but it does not work. Is there a solution?
|> I would prefer not to reread the entire file for each line ...

What you need to do is to reset the Internal Field Separator (IFS) to 'newline'.
The for loop extracts each field.  In your case, the field separator is
probably set to space, tab, and newline; therefore, the spaces in each
line are acting as a field separator and the 'for' loop is seeing each
of these characters as a separate field in the line separated by spaces.
You want it reset the IFS to newline only.  Following are a couple of
examples.

Try this...

$ IFS="               # Just hit the enter key here#

Quote:> "

$ for i in `cat file`; do
Quote:> echo $i
> done

a b c
d e f
$

If you want to save the old IFS value prior to changing it, try

$ OLD_IFS=${IFS}
$ IFS="

Quote:> "

$ for i in `cat file`; do
Quote:> echo $i
> done

a b c
d e f
$ IFS=${OLD_IFS}
--
+==============================+=============================================+
| David W. Pledger             | S T R A T E G I C   D A T A   S Y S T E M S |

| Custom Database Applications | Phone (513)748-2460, (800)253-5624 ext 2940 |
 
 
 

for i in `cat file` ... separator=\n instead of whitespace

Post by Georges Kessel » Sun, 14 Nov 1993 02:00:17


Quote:> Arne Juul has already suggested this (which also avoids the call to cat):

>         while read i
>         do echo "$i"
>         done < file

Thanks all for the tip, I really should have found out myself, but sometimes
there is some internal blocking in the brain :-)
It's already running fine (the script I mean).

Georges.
--
Georges Kesseler


IRC: Gunstick

 
 
 

1. "cut" field separator-- whitespace?

Can the cut command use more than just a single character as its field
delimiter? In particular, can it use "whitespace" (one OR MORE space
characters) instead?

In a shell script, I want to pick out words separted by space
characters (I happen to be working with the output of ipfwadm -F -l
-e). Trouble is, the number of spaces separating successive words
varies.

Cut lets you specifiy a delimiter character. But apparently insists
that each and every occurrence of that character starts a new field.
For instance, if I have

good  better  best

with 2 space characters between each word, I'd like to regard this as
3 fields, "best" being the 3rd. I think cut calls it 5 fields and
makes "best" the 5th.

Given the ordinal of a field that belongs to a set of
whitespace-separated fields, can cut be made to locate that field? If
not, can you suggest another way?

2. POSIX function for time with millisecs

3. Solaris 8 box: cat file | rsh solaris_2.6_box "cat > /dev/null" SLOW

4. XFree86 VESA driver question: UseFBdev does not work?

5. cat file > /dev/cua1 vs. cp file /dev/cual ?

6. What is the best version of Linux?

7. 'cat file' but only if 'file' exist

8. Segmentation Fault?

9. newbie: how to extract part of file by separator

10. Concatentate files with separator

11. Splitting file at separator to mail the chunks

12. Advice: cat $FILE|$SENDMAIL -oi -t OR sendmail -oi -t <$FILE

13. Is "sh file" equivalent to "cat file | sh"