Process each line of a file in bash

When I want to do something with each line in a file I usually write

cat my_file | while read a
do
gzip "$a"
done

The gzip is just an example it can be anything.

What I'm wondering is if there is another way to get the lines than cat file | while read?

3 Answers

You don't need to use cat:

while read line; do echo "$line"
done < my_file

I don't think there's a simpler way though.

2

The simplest, I think, would be to use xargs, e.g.,

xargs -L1 gzip < my_file

The -L1 option tells xargs to process one input line at a time. You might take a look at GNU parallel, too, which is very similar to xargs but more powerful in some situations.

IFS="\n\b"; for i in $MYROWOFCONTENT; do echo $i; done;

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like