Re: Howto read file line-by-line in bash



2008-04-25, 08:03(-07), Guru:
[...]
Try using this..

OLDIFS=$IFS
IFS="|"
for line in `cat test.data`;
do
echo $line;
done
IFS=$OLDIFS

This is simple and crisp.
[...]

And you oversaw three more problems: empty lines are discarded,
try it with a line containing "*", and if IFS was previously
unset, it becomes set to the empty string which has a totally
different meaning.

Using loops in shells always leads to this kind of corner case
problems. Shells are not meant to be used like that, avoid loops
to process text.

--
Stéphane
.



Relevant Pages

  • Use of ":" (was: Re: mtab very long)
    ... substitutes the value. ... then there are places where you need empty statements. ... older shells didn't have the "not" operator for program exit ...
    (Debian-User)
  • Re: PH/W? - Boots Survey - ?1k - weekly draw
    ... that time he has never offered me anything so appetizing as a dead rat!...He ... did offer me a pile of empty snail shells once....but they had been sucked ...
    (uk.rec.competitions)
  • Re: 1650, scientific letter to the Queen
    ... Take four large half-cut ball pieces and empty the inside. ... We only need the outer shells. ... half-balls and place them around a metal stick. ... at it's back forward with forward-rolling propellers. ...
    (sci.astro)
  • 1650, scientific letter to the Queen
    ... I would like to present an invention, ... Take four large half-cut ball pieces and empty the inside. ... We only need the outer shells. ... at it's back forward with forward-rolling propellers. ...
    (sci.astro)