Processing lines from a file
From FBSD_tips
Unix loves and lives on text files. Sometimes you may want to keep some important stuff in a text file. Here is a code snippet that will perform some action for each line in a file.
cat file.txt | while read L ; do echo "command for line $L"; done
Why make it complicated? You might ask. Well there are simpler solutions for simple cases, but with a scripted approach you can take into account and address the more complicated cases. For instance, a file that has 2 columns, #1 a file name and #2 an md5 command output and you want to compare the current file's md5 with the recorded one.
cat file.txt | while read L
do
L1=`awk '{print $1]'`
L2=`awk '{print $2]'`
if [ -s $L1 ]
then
MD=` md5 $L1`
echo "Recorded md5 = $L2, actual = $MD"
else
echo "file not present"
fi
done
This is just one made up example to demonstrate the "file pipe into a while read" idiom.
Gongo 23:11, 9 February 2008 (UTC)
