shell scripting (bash) reading lines out of files
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix administration > shell scripting (bash) reading lines out of files




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    shell scripting (bash) reading lines out of files  
Rob Baxter


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:02 PM

I need to read into a script the contents of each line of a text file
for use elsewhere within my script before I move onto the next line in
the file.  What utility (utilities) do I have at my disposal which
will allow me to take out each line of my file one at a time?  I'm
using Redhat 8.0 and BASH.  Any help is appreciated as always, thanks,

Rob





[ Post a follow-up to this message ]



    Re: shell scripting (bash) reading lines out of files  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:02 PM

On Tue, 16 Sep 2003 at 20:01 GMT, Rob Baxter wrote:
quote:
> I need to read into a script the contents of each line of a text file > for use elsewhere within my script before I move onto the next line in > the file. What utility (utilities) do I have at my disposal which > will allow me to take out each line of my file one at a time? I'm > using Redhat 8.0 and BASH. Any help is appreciated as always, thanks,
To read a file line by line: while IFS= read line do : do whatever with $line done < FILENAME -- Chris F.A. Johnson http://cfaj.freeshell.org ======================================== =========================== My code (if any) in this post is copyright 2003, Chris F.A. Johnson and may be copied under the terms of the GNU General Public License




[ Post a follow-up to this message ]



    Re: shell scripting (bash) reading lines out of files  
Rob Baxter


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:02 PM

Thanks guys - I have what you've written working now but don't
understand how it is working.  I've used a greater than sign to
redirect the output of several different utilities to a file in the
past, but I don't know what a less than sign is doing or how it works.
Can you explain that a bit?





[ Post a follow-up to this message ]



    Re: shell scripting (bash) reading lines out of files  
Andreas Kahari


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:02 PM

In article <baea5fa.0309170633.35562e77@posting.google.com>, Rob Baxter wrote:
quote:
> Thanks guys - I have what you've written working now but don't > understand how it is working. I've used a greater than sign to > redirect the output of several different utilities to a file in the > past, but I don't know what a less than sign is doing or how it works. > Can you explain that a bit?
The read command that I used in my post reads data from standard input. Using "<data" I can replace standard input with the contents of the file "data", simply speaking. Any command or program that expects to be fed data on standard input may have its data fed from a file in this way. For example, the tr command may be used this way: tr '$' '£' <myfile >new.myfile which will replace all occurances of '$' with '£' in the file "myfile". The result is written to "new.myfile". If no redirection had been doen, all input and output would have been to/from the console. Redirection of standard input and standard output is a basic concept of shell scripting. -- Andreas Kähäri




[ Post a follow-up to this message ]



    Re: shell scripting (bash) reading lines out of files  
Doug Freyburger


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:02 PM

Rob Baxter wrote:
quote:
> > I need to read into a script the contents of each line of a text file > for use elsewhere within my script before I move onto the next line in > the file.
Standard idiom is like this: cat file | while read word1 word2 word3 ... ; do stuff done If you only use one variable, it gets the entire line. For fixed field files, I like to use meaningfull variables mixed with placeholder ones like "device directory three four five six". Of course if it's colon separated chop it with tr ":" " " file | read ... and so on.




[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 01:23 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register