2
$ awk '$2 - /^Tom$/ {print}' names.txt

I am trying to search for a file that has the 2nd field matching Tom.

But I am getting this error:

awk: syntax error at source line 1
 context is
    $2 - /^Tom$/ >>>  { <<< 
  • 1
    I don't see how it could cause the specific error that you have reported, but the regex comparison operator should be tilde (~) not minus (-). Although if you are testing for a literal match, $2 == "Tom" would be simpler. – steeldriver Jan 17 '17 at 2:00
  • Question, are you using non-english input language ? – Sergiy Kolodyazhnyy Jan 17 '17 at 3:21
4

You have syntax error. Should be

awk '$2 ~ /Tom/ {print}' names.txt

or

awk '$2 == "Tom" {print}' names.txt

Note that {print} is unnecessary. Awk will print lines for which matching condition is true automatically.

$ awk '$2 =="Tom"' input.txt                                                                                             
Edison, Tom

$ cat input.txt                                                                                                          
Washington, George
Edison, Tom
Tesla, Nik

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.