|
Home > Archive > Unix administration > January 2004 > shell scripting (bash) reading lines out of files
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
shell scripting (bash) reading lines out of files
|
|
| Rob Baxter 2004-01-23, 5: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
| |
| Chris F.A. Johnson 2004-01-23, 5: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
| |
| Rob Baxter 2004-01-23, 5: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?
| |
| Andreas Kahari 2004-01-23, 5: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
| |
| Doug Freyburger 2004-01-23, 5: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.
|
|
|
|
|