5

I have a file like one below

var     3   2014   string
var1    4   2011   string4
var2    6   1999   string2
var3    1   2016   string6

Then i have this while read loop to compare one of the columns to a number then echo something. However, instead of echoing my desired phrase, it echoes something else.

while read num
do
if [ "$num" = "0" ]; then
echo "Number is equal to zero"
else
echo "number is not equal to 0"
fi
done < home/dir/file.txt | awk '{print $2}' 

instead of echoing the above, it echoes the 2nd column of the file.

  • I can't actually reproduce the problem when running the script as-is, but in any case you have other problems with your script. Mainly, you do read num and expect the result to be a single number (which you compare against the string "0"), but you are actually reading an input file with 4 columns so $num will be a string with 4 columns in it. – Celada Jun 11 '15 at 7:19
  • 1
    Not tested, but I think your problem is that the pipe will be done after the while loop? Anyway, why not all in awk: awk '{if ($2==0){print "equal"}else{print "not"}}' file.txt ? – fredtantini Jun 11 '15 at 7:20
  • As presented, that code snippet would print the second word from each line produced by the while ... done loop. I.e.: is x4 – Henk Langeveld Jun 12 '15 at 11:03
5

you should try

awk '{print $2}' home/dir/file.txt | while read num
do
  if [ "$num" = "0" ]; then
    echo "Number is equal to zero"
  else
    echo "number is not equal to 0"
  fi
 done 

for a mixed awk/bash solution.

As other have pointed out, awk redirection occur later.

3

It looks like you're trying to redirect the output of home/dir/file.txt | awk '{print $2}' to the while loop;

first I guess that the correct path should be /home/dir/file.txt (however this is just an assumption);

second /home/dir/file.txt | awk '{print $2}' doesn't redirect the content of /home/dir/file.txt to awk, while < /home/dir/file.txt awk '{print $2}' does;

third, you're redirecting the output of the command as a file, but it's a string, and you should redirect it as it: <<< "$(< /home/dir/file.txt awk '{print $2}')".

Also, alternatively, you could pipe the output of the command directly to the while loop instead: < /home/dir/file.txt awk '{print $2}' | while read num.

while read num
do
    if [ "$num" = "0" ]; then
    echo "Number is equal to zero"
else
    echo "number is not equal to 0"
fi
done <<< "$(< /home/dir/file.txt awk '{print $2}')"

or

< /home/dir/file.txt awk '{print $2}' | while read num
do
    if [ "$num" = "0" ]; then
    echo "Number is equal to zero"
else
    echo "number is not equal to 0"
fi
done
2

Since I can't comment, just wondering, why people have not used IFS

while IFS=" " read var num year string
do
  if [[ $num -eq 0 ]]; then
    echo "Number is equal to zero"
  else
    echo "number is not equal to 0"
  fi
 done < home/dir/file.txt

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.