Skip to content
Advertisement

What is a better way to read each line in a file in linux?

cat  swerror.txt | while read Wrapper 
do
......
done

Hi all, I want to read each line in a file and then make a whole url to do some thing. The above scripts worked, but what is a better way to read file line by line? Thanks.

Advertisement

Answer

This is a nice example of the useless use of cat, simply use IO redirection:

while read -r Wrapper 
do
    ...
done < swerror.txt

The use of -r in read is explained here.

It saves you the creation of a pipe and a process (cat) and will thus be more efficient. Although as @TomFenech says, svn (the inner loop command you first gave) will probably be the dominant factor since it requires network traffic. And anyway, in most cases the runtime will not improve much by optimizing pipes, etc.

Advertisement