Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Reading line by line



On Wed, Apr 11, 2007 at 11:13:21AM -0700, Micah Cowan wrote:
> sac wrote:
> > 2. Like Micah J. Cowan pointed out, 
> > 
> > while read i
> > do
> > echo $i
> > done < filename
> 
> As Stephane points out, -r is preferable, as it disables
> backslash-escaping. IFS= should not be necessary--and isn't, in zsh--but
> dash, bash and ksh all violate POSIX (as I read it) by inserting new
> field separators after splitting, instead of using the original
> separation; so using IFS= is portable.
[...]

Don't know what you mean, but IFS= is necessary even with zsh,
and the behavior is POSIX in every shell AFAICT, could you
please expand.

$ echo '  a  ' | read -r
$ echo "<$REPLY>"
<a>
$ echo '  a  ' | IFS= read -r
$ echo "<$REPLY>"
<  a  >

zsh also has:

IFS=$'\n\n' read -rAd '' argv
argv=("${(@)argv[1,-2]}")
for i do
  print -r -- $i
done

Or to loop over non-empty lines:

for i in ${(f)"$(cat filename)"}; do
  print -r -- $i
done

Myself, I go for

@lines = <>;

or 

while (<>)

or

perl -ne '...'


:)

-- 
Stéphane



Messages sorted by: Reverse Date, Date, Thread, Author