I am pretty new to bash commands, and I’m trying to make a for statement that prints out each line with a value of 3 in the 6th column (space delimited) to a file. My syntax looks like this:
for i in `cat test`
do
section=`echo ${i} | cut -d ' ' -f 6`
line=`echo ${i}`
if [ ${section} == "3" ];
then
echo ${line} > test.out
fi
done
Unfortunately, this doesn’t work… Can anyone tell me what’s wrong with this?
You’re not reading the file the right way.
You can debug it by trying:
you will see that it prints each word on a new line, because the for-loop is actually iterating over each word in the file, instead of each line.
To read a file line by line use the following:
By the way, if you want an alternative solution, you can use this
awkone-liner: