LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash: read a text file line by line. (https://www.linuxquestions.org/questions/programming-9/bash-read-a-text-file-line-by-line-890424/)

stf92 07-07-2011 08:42 AM

Bash: read a text file line by line.
 
bash 3.1.17(2)

Hi:
I'm trying do write a shell script which must operate on each line of an ASCII text file. So, all the code must be inside a loop, and inside the loop, the first thing should be to read the next line from the file. I have the bash read command. But it reads from stdin. Any way to make read from a file? Thanks.

druuna 07-07-2011 08:49 AM

Hi,

Have a look at this:
Code:

#!/bin/bash

while read ONELINE
do
  echo "And the next line is"
  echo "$ONELINE"
done < infile

Testrun:
Code:

$ cat infile
line 1
line 2
line 3

$ ./foo.sh
And the next line is
line 1
And the next line is
line 2
And the next line is
line 3

The while read ... part can take one or more variables (the above uses just one).

Hope this helps.

stf92 07-07-2011 09:02 AM

It certainly does. I now have the skeleton of my program. This can seem as a easy attitude on my part, but I can assure you I learn from it. Because I'm about to ask one more question.

I have now the line in my power. It is in ONELINE. The line will be of the form
<blank space>INDEX NN mm:ss.ss
How could I capture the part mm:ss.ss?

I want to do this in a shell script language, because I'm learning bash scripting and this would further my knowledge. Regards.

druuna 07-07-2011 09:11 AM

Hi,

You can use more then one variable after the while read statement:
Code:

#!/bin/bash

while read FIRST SECOND THIRD
do
  echo "-----------------"
  echo "$FIRST"
  echo "$SECOND"
  echo "$THIRD"
done < infile

$ cat infile
 INDEX1 XX mm1:ss.ss
 INDEX2 YY mm2:ss.ss
 INDEX3 ZZ mm3:ss.ss

$ ./foo
-----------------
INDEX1
XX
mm1:ss.ss
-----------------
INDEX2
YY
mm2:ss.ss
-----------------
INDEX3
ZZ
mm3:ss.ss

Hope this helps.

stf92 07-07-2011 09:28 AM

How stupid. The read syntax ends [name ...]. Thanks a thousand times.

druuna 07-07-2011 09:48 AM

You're welcome :)

MTK358 07-07-2011 09:49 AM

Note that you can also pipe the output of a command into a loop:

Code:

command | while read
do
    # stuff
done

The problem is that this will run the loop in a subshell, so that if it modifies any variables, the changes will not stay once the loop exits. If that's a problem, you can use this instead:

Code:

while read
do
    # stuff
done < <(command)

The <(command) syntax creates a named pipe, connects the command's output to it, and evaluates to the path to the named pipe.

rameshpaul_cog 06-11-2012 02:52 PM

[QUOTE=druuna;4407869]Hi,

You can use more then one variable after the while read statement:
Code:

#!/bin/bash

while read FIRST SECOND THIRD
do
  echo "-----------------"
  echo "$FIRST"
  echo "$SECOND"
  echo "$THIRD"
done < infile

$ cat infile
 INDEX1 XX mm1:ss.ss
 INDEX2 YY mm2:ss.ss
 INDEX3 ZZ mm3:ss.ss

$ ./foo
-----------------
INDEX1
XX
mm1:ss.ss
-----------------
INDEX2
YY
mm2:ss.ss
-----------------
INDEX3
ZZ
mm3:ss.ss

Hope this helps.[/Really this stuff is awesome.. i was just posting a thread for my requirement like the same, but i got this one from Druuna...thanks alot]


All times are GMT -5. The time now is 06:49 PM.