It is currently Thu, 20 Jan 2022 04:44:56 GMT



 
Author Message
 Read a file line after line
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



 Wed, 16 Jun 2004 18:17:15 GMT   
 Read a file line after line

Well, here's a BASH example for ya.

[compiler@1984:~]
$ cat test
A
B
C
D
[compiler@1984:~]
$ for PARA in `cat test`; do echo $PARA; done
A
B
C
D
[compiler@1984:~]
$

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



 Wed, 16 Jun 2004 19:23:25 GMT   
 Read a file line after line

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



 Wed, 16 Jun 2004 23:34:37 GMT   
 Read a file line after line
Chris F.A. Johnson says:

or

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

which is two times faster:

[tri10o@unrafined tri10o]$ date; oldIFS=$IFS; IFS=$'\n'; for PARA in
`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
[tri10o@unrafined tri10o]$ date; while read PARA; do echo $PARA; done >
/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   |
email: tri...@poland.com or tri...@bsod.org        | open    |
                                                   |_DISPLAY_|



 Fri, 18 Jun 2004 16:21:46 GMT   
 Read a file line after line

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



 Fri, 18 Jun 2004 18:52:29 GMT   
 Read a file line after line

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';
[compiler@1984:~/java/JNotePad]
$echo $J

[compiler@1984:~/java/JNotePad]
$echo $K # <-- K is not set

[compiler@1984:~]
$export J=$'\n\n';
[compiler@1984:~]
$echo $J

[compiler@1984:~]
$export Q=' hello'
[compiler@1984:~]
$export W=' world'
[compiler@1984:~]
$echo $Q$W
hello world
[compiler@1984:~]
$echo $Q$J$W
hello world
[compiler@1984:~]
$

John



 Sat, 19 Jun 2004 16:11:41 GMT   
 Read a file line after line

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

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



 Sat, 19 Jun 2004 17:00:34 GMT   
 
   [ 7 post ] 

Similar Threads

1. read a file line by line in csh script

2. reading a file line by line

3. read a file line by line

4. Read a file line by line

5. Reading a file line by line

6. Reading flat files, line by line

7. read from stdin while reading line by line a file

8. line by line reading of the lines in list

9. How do I read a file line by line in a csh script?

10. HELP: How do I read a file line-by-line without getting spaces stripped?


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software