LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-07-2011, 08:42 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
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.

Last edited by stf92; 07-07-2011 at 08:43 AM.
 
Old 07-07-2011, 08:49 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 07-07-2011, 09:02 AM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
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.

Last edited by stf92; 07-07-2011 at 09:05 AM.
 
Old 07-07-2011, 09:11 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
1 members found this post helpful.
Old 07-07-2011, 09:28 AM   #5
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
How stupid. The read syntax ends [name ...]. Thanks a thousand times.
 
Old 07-07-2011, 09:48 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
You're welcome
 
Old 07-07-2011, 09:49 AM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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.
 
Old 06-11-2012, 02:52 PM   #8
rameshpaul_cog
LQ Newbie
 
Registered: May 2012
Posts: 19

Rep: Reputation: 0
[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]
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] open two text files , read them line by line and update parameters of the 3rd file rastin_nz Programming 17 10-20-2010 07:10 PM
bash : read every line from text file starting at given line number quadmore Programming 4 02-20-2009 12:29 PM
bash - read or write to specific line in text file? babag Programming 11 08-23-2008 01:44 PM
help with c program to read each line from text file, split line , process and output gkoumantaris Programming 12 07-01-2008 12:38 PM
read line by line form text file in java. spank Programming 1 10-18-2006 02:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration