Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

hi all 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

share|improve this question
    
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
add comment

2 Answers 2

up vote 2 down vote accepted

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....

share|improve this answer
add comment

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).

#!/bin/bash

LINE="123 aaa ddd ggg 0 yyy hhh"

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

Does that do what you want?

It would be useful if you provided more example input.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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