Unix/Linux shell script FAQ: How do I write a Unix or Linux shell script where I “do something” for every line in a text file?
Solution: A simple way to process every line in a text file is to use a Unix/Linux while
loop in combination with the Linux cat command, like this:
file=myfile.txt # handle line breaks properly; needed when a line has spaces or tabs IFS=$'\n' for i in `cat $file` do # add your logic here echo "$i" done
Now all you have to do is put some code inside the while
loop. For instance, I was just given a text file, and I wanted to add a colon character and some extra text after every line in the file. There are a million ways to do this, but a really easy way is to use this Unix shell script approach. I can’t share everything I just did, but part of my file processing looked like this:
file=myfile.txt for i in `cat $file` do # add your logic here echo "$i:\t\t (more text here)" done
I then ran this script like this:
sh script.sh > output-file
Of course I could have also hard-coded the output file, but that’s up to you.
In summary, I hope this Unix/Linux shell script on how to process every line in a text file is helpful.