Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

First I aplogize for this idiot question .

I want to print a field if he is equals to a value .

The problem is that I'm using a non default field sperator and I didn't understand how to make it work .

 awk '{FS = "/" ; if($3=="value" print $3 }'

Thanks.

share|improve this question
    
To assign an awk "special" variable like FS, either use a command line option like @Thor demonstrated, or use the BEGIN block. The way you've shown in the question means you reassign it for every line of the file. Also, you're missing the close-parentheses for your if condition. –  glenn jackman Aug 4 '12 at 22:10

1 Answer 1

up vote 2 down vote accepted

The more awkish way is like this:

awk -F/ '$3 == "value" { print $3 }'

Explanation

  • -F specifies the field separator
  • Each block is executed based on the preceding condition, if non is given it is always true, the condition here is $3 == "value".
  • Regular expressions can be used instead if == is replaced with ~.
share|improve this answer

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.