18

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.

3
  • 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. Commented Jun 11, 2015 at 7:19
  • 2
    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 ? Commented Jun 11, 2015 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 Commented Jun 12, 2015 at 11:03

3 Answers 3

12

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.

6

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
6

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.