Read a file line after line

Read a file line after line

Post by Yve » Sun, 30 Dec 2001 19:17:15



Hi,

I got a file and I must assign every line in this file at the same
variable with a little loop.

So for example,

I got a file with :
A
B
C
D

and the variable PARA must be A after B after C and after D... Any
idea?

I got 402 lines in the file...

Thanks a lot,

Fred

 
 
 

Read a file line after line

Post by John Daviso » Sun, 30 Dec 2001 20:23:25



> Hi,

> I got a file and I must assign every line in this file at the same
> variable with a little loop.

> So for example,

> I got a file with :
> A
> B
> C
> D

> and the variable PARA must be A after B after C and after D... Any
> idea?

> I got 402 lines in the file...

> Thanks a lot,

> Fred

Well, here's a BASH example for ya.


$ cat test
A
B
C
D

$ for PARA in `cat test`; do echo $PARA; done
A
B
C
D

$

So you see, PARA gets set to whatever the contents of the line is, once for
each line in the file.  In my example, I just echoed the value to show you
it works.

Hope that is what you wanted!

John

 
 
 

Read a file line after line

Post by Chris F.A. Johnso » Mon, 31 Dec 2001 00:34:37




> > I got a file and I must assign every line in this file at the same
> > variable with a little loop.

> > So for example,

> > I got a file with :
> > A
> > B
> > C
> > D

> > and the variable PARA must be A after B after C and after D... Any
> > idea?

> > I got 402 lines in the file...

> Well, here's a BASH example for ya.


> $ cat test
> A
> B
> C
> D

> $ for PARA in `cat test`; do echo $PARA; done
> A
> B
> C
> D

> $

> So you see, PARA gets set to whatever the contents of the line is, once for
> each line in the file.  In my example, I just echoed the value to show you
> it works.

That will only work if there is one word per line; otherwise it will
be set to each word in turn.

        while read PARA
        do
           ## each line will be stored in PARA
        done

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2001, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

Read a file line after line

Post by Triten » Tue, 01 Jan 2002 17:21:46


Chris F.A. Johnson says:

Quote:>> $ for PARA in `cat test`; do echo $PARA; done
> That will only work if there is one word per line; otherwise it will
> be set to each word in turn.

>    while read PARA
>    do
>       ## each line will be stored in PARA
>    done

or

oldIFS=$IFS; IFS=$'\n';
for PARA in `cat test`; do echo $PARA; done;
IFS=$oldIFS

which is two times faster:


`cat testfile`; do echo $PARA; done > /dev/null; IFS=$oldIFS; date
pon gru 31 08:30:07 CET 2001
pon gru 31 08:30:10 CET 2001

/dev/null < testfile; date
pon gru 31 08:30:12 CET 2001
pon gru 31 08:30:18 CET 2001

testfile was created by:
for i in `seq 15000`; do echo "word two words no one can tell"; done >
testfile
and has about 454k

ps. hello group, this is my first letter here, but not last... i hope
that ...and i've got something for you ;)

echo $'\x1B\x5B\x31\x3B\x33\x37\x3B\x34\x34\x06D\x48\x61\x70\x70\x79\x1B\x5B\x31\x3B\x33\x38\x3B\x34\x33\x06D\x20\x6e\x65\x77\x20\x1B\x5B\x31\x3B\x33\x39\x3B\x34\x32\x06D\x59\x65\x41\x72\x20\x1B\x5B\x31\x3B\x33\x31\x3B\x34\x30\x06D\x30\x37\x44\x32\x68\x20\x1B\x5B\x31\x3B\x33\x33\x3B\x34\x34\x06D\x3b\x29\x1B\x5B\x30\x3B\x33\x37\x06D'

--
first name: Jacek  last name: Pospycha3a           .-photo---.
nick: triteno      web: http://tri10o.republika.pl | can't   |

                                                   |_DISPLAY_|

 
 
 

Read a file line after line

Post by Chris F.A. Johnso » Tue, 01 Jan 2002 19:52:29



> Chris F.A. Johnson says:
> >> $ for PARA in `cat test`; do echo $PARA; done
> > That will only work if there is one word per line; otherwise it will
> > be set to each word in turn.

> >       while read PARA
> >       do
> >          ## each line will be stored in PARA
> >       done

> or

> oldIFS=$IFS; IFS=$'\n';
> for PARA in `cat test`; do echo $PARA; done;
> IFS=$oldIFS

> which is two times faster:

But which doesn't work if the lines contain more than one word (as I
already pointed out).

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2001, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

Read a file line after line

Post by John W. Daviso » Wed, 02 Jan 2002 17:11:41




>>Chris F.A. Johnson says:

>>>>$ for PARA in `cat test`; do echo $PARA; done

>>>That will only work if there is one word per line; otherwise it will
>>>be set to each word in turn.

>>>    while read PARA
>>>    do
>>>       ## each line will be stored in PARA
>>>    done

>>or

>>oldIFS=$IFS; IFS=$'\n';
>>for PARA in `cat test`; do echo $PARA; done;
>>IFS=$oldIFS

>>which is two times faster:

> But which doesn't work if the lines contain more than one word (as I
> already pointed out).

Actually, at least in my version of Bash (2.05.0(1)-release), that idea
  *will* work.  The IFS variable determines where it breaks apart the
data.  By default it splits on space, tabs and newlines, but if you set
it to just a newline it will split it line by line.  I didn't test the
above code, but I have done this myself using something like
IFS='
'; for x in `cat test`; do echo $x; done

I've never seen the syntax =$'\n' before, and I couldn't get it work.

$export J=$'\n';

$echo $J


$echo $K # <-- K is not set


$export J=$'\n\n';

$echo $J


$export Q=' hello'

$export W=' world'

$echo $Q$W
hello world

$echo $Q$J$W
hello world

$

John

 
 
 

Read a file line after line

Post by Chris F.A. Johnso » Wed, 02 Jan 2002 18:00:34





> >>Chris F.A. Johnson says:

> >>>>$ for PARA in `cat test`; do echo $PARA; done

> >>>That will only work if there is one word per line; otherwise it will
> >>>be set to each word in turn.

> >>>       while read PARA
> >>>       do
> >>>          ## each line will be stored in PARA
> >>>       done

> >>or

> >>oldIFS=$IFS; IFS=$'\n';
> >>for PARA in `cat test`; do echo $PARA; done;
> >>IFS=$oldIFS

> >>which is two times faster:

> > But which doesn't work if the lines contain more than one word (as I
> > already pointed out).

> Actually, at least in my version of Bash (2.05.0(1)-release), that idea
>   *will* work.  The IFS variable determines where it breaks apart the
> data.  By default it splits on space, tabs and newlines, but if you set
> it to just a newline it will split it line by line.  I didn't test the
> above code, but I have done this myself using something like
> IFS='
> '; for x in `cat test`; do echo $x; done

You're quite right, of course. My fingers were faster than my brain.

> I've never seen the syntax =$'\n' before, and I couldn't get it work.

> $export J=$'\n';

> $echo $J

I think it worked; try:

        echo "$J"
or:
        echo "qwerty${J}uiop"

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License