MandrivaUsers.org : reading a text file, line by line with bash - MandrivaUsers.org

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

reading a text file, line by line with bash [solved]

#1 User is offline   santner 

  • Linux since 8/03
  • Group: Members
  • Posts: 212
  • Joined: 18-May 04

Posted 09 January 2005 - 03:44 AM

Bash is awesome, the only problem I have is that I have yet to find a simple way to read a basic text file from within a bash script one line at a time. For example, say my text file contains:

'Christmas Eve'
'Christmas Morning'
'New Years Eve'

So from my bash script I would like to be able to read each entire line, one at a time and save it to a variable within the script. I have googled and still can't find the answers. Please help! :D/>
0

#2 User is offline   aru 

  • Mandriva Guru
  • Group: Members
  • Posts: 2,150
  • Joined: 04-December 02

Posted 09 January 2005 - 11:37 AM

At first thought I can type two ways, I prefer the former because it doesn't use external commands:
while read line; do 
    echo $line # or whaterver you want to do with the $line variable
done < FILE


or

cat FILE | while read line; do 
    echo $line # or whaterver you want to do with the $line variable
done


hth
The text you've read above is supposed to be in english;
oops! btw, the code in this post -if any- is released under the GNU GPL v2 or later, that means among other things that it hasn't any warranty.


- The Unix philosophy in one lesson: KEEP IT SIMPLE, STUPID! -- ES. Raymond. "TAOUP"

You might want to take a look to our Musb FAQs and to "THE" FAQs... and remember "/usr/bin/man" is your best friend, use it!

:wq
0

#3 User is offline   aru 

  • Mandriva Guru
  • Group: Members
  • Posts: 2,150
  • Joined: 04-December 02

Posted 09 January 2005 - 11:55 AM

Here is another example that has come up to my mind, this time I'm using the Field separator variable (IFS) set to "new line", and an array to store each line of the file into a field of an array named "lines":
old_IFS=$IFS
IFS=$'\n'
lines=($(cat FILE)) # array
IFS=$old_IFS


hence you can call each line by asking for its relative number:
echo ${line[4]} # will echo line number 4 (line numbering start with 0)

echo ${line[@]} # will print all the lines.

echo ${line[#]} # will print the size of the array (the total line numbering)


there are many other ways, just use the one that better fits your needs


hth

This post has been edited by aru: 09 January 2005 - 11:57 AM

The text you've read above is supposed to be in english;
oops! btw, the code in this post -if any- is released under the GNU GPL v2 or later, that means among other things that it hasn't any warranty.


- The Unix philosophy in one lesson: KEEP IT SIMPLE, STUPID! -- ES. Raymond. "TAOUP"

You might want to take a look to our Musb FAQs and to "THE" FAQs... and remember "/usr/bin/man" is your best friend, use it!

:wq
0

#4 User is offline   santner 

  • Linux since 8/03
  • Group: Members
  • Posts: 212
  • Joined: 18-May 04

Posted 10 January 2005 - 04:40 AM

awesome, exactly what I was looking for. I prefer the array style, very solid code.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

3 User(s) are reading this topic
0 members, 3 guests, 0 anonymous users