To find if field 5 has I use the:

   [[ -z ` echo $LINE  | awk '{print $5}' ` ]]

my question if there is another elegant way to verify if field 5 is zero?

THX Yael

  • provide example lines – akira Jun 27 '10 at 9:02
  • LINE=123 aaa ddd ggg ttt yyy hhh – yael Jun 27 '10 at 10:00

After length5=$(echo $LINE | awk '{ print length($5)}') the variable $lenght5 will contain the length of field 5;

Note: in your example line, if ttt were empty, yyy would be field 5, so be careful: you can use

awk -F' '

instead of awk to make the space the field delimiter, so that 2 spaces = empty field....


This will pass if the 5th field is the string 0 (and won't if it's 00, 0.0, or it's absent etc).


LINE="123 aaa ddd ggg 0 yyy hhh"

if [[ "0" = `echo ${LINE}  | awk '{print $5}'` ]]
   echo "5th field is string '0'"
   echo "5th field isn't string '0'"

Does that do what you want?

It would be useful if you provided more example input.

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.