I am trying to run the following command on Centos however I get an error? Unexpected token near '('

gawk { if($0=="id323"){print $0} } doc.txt
  • 1
    Try adding quotes around {...}. Unrelated: you can just run gawk '$0 == "id232"' doc.txt. Or grep -x id232 doc.txt. – Satō Katsura Apr 6 '17 at 18:23

You need to put the actual awk script in strong quotes to prevent the shell from seeing them and trying to parse them before passing the command in to awk:

gawk '{ if( $0 == "id123"){ print $0 } }' doc.txt

As an aside, you can do this slightly more elegantly:

gawk '$0 == "id123" { print $0 }' doc.txt
  • 4
    ... And gawk '$0 == "id232"' doc.txt is even more elegant, as I pointed out above. :) – Satō Katsura Apr 6 '17 at 18:35

I don't know what you want to achieve. This code only lets you find out whether there are lines containing id323 in the document. You don't gain any further information from this. So a simple grep would be sufficient.

Futhermore: I don't see that the given solution is correct at all. I think it should read:
gawk '$0 ~/id323/ { print $0 }' doc.txt
or even shorter
gawk '$0 ~/id323/' doc.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.