Re: Howto read file line-by-line in bash
- From: Guru <guru1309@xxxxxxxxx>
- Date: Fri, 25 Apr 2008 08:03:14 -0700 (PDT)
On Mar 20, 6:04 am, Viatly <postoronnim...@xxxxxxx> wrote:
On 20 mrt, 10:49, Stephane CHAZELAS <this.addr...@xxxxxxxxxx> wrote:
2008-03-20, 02:38(-07), Viatly:
The content of test.data is
-bash-3.2# cat test.data
line1
line2
line3
It is important that there is no trailing EOL at the end of file.
I read test.data with the following script:
-bash-3.2# cat test.sh
#!/bin/bash
while read line
do
echo "$line"
done < "test.data"
-bash-3.2# ./test.sh
line1
line2
That is, "line3" is lost.
Questions:
1. What is a nice way to fix this code?
The nicest way is to avoid while read loops in shells.
Why? In fact what I need is
while read line
do
# do some processing
done < "test.data"
2. The code of the script pretends to be a stdandard way if reading
text file line-by-line because it is recommended by respected
resources (e.g.http://bash-hackers.org/wiki/doku.php/tests/bashfaq).
Provided the code is ok, does it mean that a typical text file in Unix/
Linux should have EOL at the end?
"read" returns false if a full line is not read, but $line will
contain those extra characters after the last NL character
while IFS= read -r line; do
printf '%s\n' "$line"
done < test.data
printf %s "$line"
Will do it, but
cat test.data
Do you mean:
for line in `cat test.data`;
do
echo $line;
done
In this case if the line contains words separated by a whitespace,
this whitespace will be used as a separator. Which I do not need.
Note that a file that doesn't end in a NL character is not a
text file as per the POSIX definition of a text file. (that
means for instance that the behavior of a text utility
processing it is unspecified most of the time).
This is good argument. So, the problem is not in code, but rather in
ill-formed text file. Right?
--
Stéphane
Hi Viatly,
Try using this..
OLDIFS=$IFS
IFS="|"
for line in `cat test.data`;
do
echo $line;
done
IFS=$OLDIFS
This is simple and crisp.
Rgds
Gaurav S
.
- Follow-Ups:
- Re: Howto read file line-by-line in bash
- From: Stephane CHAZELAS
- Re: Howto read file line-by-line in bash
- Prev by Date: Re: Lint like report on old sources - how to let report 'point' to the new src lines?
- Next by Date: how to make a script of this one-liner...
- Previous by thread: Lint like report on old sources - how to let report 'point' to the new src lines?
- Next by thread: Re: Howto read file line-by-line in bash
- Index(es):
Relevant Pages
|