136

I'm learning awk from The AWK Programming Language and I have a problem with one of the examples.

If I wanted to print $3 if $2 is equal to a value (e.g.1), I was using this command which works fine:

awk '$2==1 {print $3}' <infile> | more

But when I substitute 1 by another searching criteria, (e.g.findtext), the command doesn't work:

awk '$1== findtext {print $3}' <infile> | more

It returns no output and I'm sure that 'findtext' exist on the input file.

I also tried this, but it does not work:

awk '$1== "findtext" {print $3}' <infile> | more

Here's my test file named 'test' and it has 9 lines and 8 fields, separated by space:

1 11 0.959660297 0 0.021231423 -0.0073 -0.0031 MhZisp
2 14 0.180467091 0.800424628 0 0.0566 0.0103 ClNonZ
3 19 0.98089172 0 0 -0.0158 0.0124 MhNonZ
4 15 0.704883227 0.265392781 0.010615711 -0.0087 -0.0092 MhZisp
5 22 0.010615711 0.959660297 0.010615711 0.0476 0.0061 ClNonZ
6 23 0.715498938 0 0.265392781 -0.0013 -0.0309 Unkn
7 26 0.927813163 0 0.053078556 -0.0051 -0.0636 MhZisp
8 44 0.55626327 0.222929936 0.201698514 0.0053 -0.0438 MhZisp
9 31 0.492569002 0.350318471 0.138004246 0.0485 0.0088 ClNonZ

Here's what I did and the output:

$awk '$8 == "ClNonZ" {print $3}' test 

$ grep ClNonZ test 
2 14 0.180467091 0.800424628 0 0.0566 0.0103 ClNonZ
5 22 0.010615711 0.959660297 0.010615711 0.0476 0.0061 ClNonZ
9 31 0.492569002 0.350318471 0.138004246 0.0485 0.0088 ClNonZ

I expect to see this which is the $3 that has "ClNonZ" in their $8.

0.180467091 
0.010615711 
0.492569002

Don't know why the awk command didn't return anything. Any thoughts?

3
  • You need to quote the string value "findtext", otherwise it's a variable name
    – evil otto
    Commented Feb 6, 2013 at 21:26
  • I tried double quotes with "findtext", but it doesn't work.. that's why it bothers me Commented Feb 6, 2013 at 21:35
  • 1
    "Doesn't work" doesn't tell us anything. Show us exact inputs, exact code, expected output, and actual output.
    – chepner
    Commented Feb 6, 2013 at 21:47

6 Answers 6

165

If you're looking for a particular string, put quotes around it:

awk '$1 == "findtext" {print $3}'

Otherwise, awk will assume it's a variable name.

8
  • I tried this but it doesn't work I don't know why. I double checked with grep and the text was in there. :( Commented Feb 6, 2013 at 21:33
  • 1
    @user1687130, I think you're going to need to show us some example input and expected output.
    – Carl Norum
    Commented Feb 6, 2013 at 21:46
  • 1
    Are you sure your data is space-separated. Might some of those spaces be tabs? Try using awk to echo a single field. Does awk '{ print $8 }' give you what you'd expect?
    – Rob Davis
    Commented Feb 6, 2013 at 22:38
  • 1
    It might be due to AWK implementation (check it with awk --version), have a look to my answer, it works in GAWK and MAWK too.
    – arutaku
    Commented Feb 6, 2013 at 22:59
  • This doesn't work when we use double quotes around the awk script. Like awk "$1 == \"findtext\" {print $3}" Commented Feb 27, 2017 at 9:30
43

This method uses regexp, it should work:

awk '$2 ~ /findtext/ {print $3}' <infile>
1
  • Thanks I was searching a way to use awk to find regex on $NF without using diabolic methods and grep ^^
    – Thibault
    Commented Jul 20, 2018 at 9:43
26

Depending on the AWK implementation are you using == is ok or not.

Have you tried ~?. For example, if you want $1 to be "hello":

awk '$1 ~ /^hello$/{ print $3; }' <infile>

^ means $1 start, and $ is $1 end.

3
  • 4
    All awk implementations support both "==" and "~".
    – Ed Morton
    Commented Feb 7, 2013 at 5:00
  • 3
    @EdMorton - OS X's awk failed to match with ==, but succeeded with ~.
    – jww
    Commented Jun 30, 2016 at 18:12
  • 2
    @jww Failed to match what with what? These are equivalent: $1 == "hello" and $1 ~ /^hello$/. You should never do $1 ~ "^hello$" as shown in this answer as it's using a string in a regexp context and so awk has to convert the string to a regexp before using it and that has side-effects (man awk).
    – Ed Morton
    Commented Jun 30, 2016 at 18:51
9

This is more readable for me

awk '{if ($2 ~ /findtext/) print $3}' <infile>
3

My awk version is 3.1.5.

Yes, the input file is space separated, no tabs.

According to arutaku's answer, here's what I tried that worked:

awk '$8 ~ "ClNonZ"{ print $3; }' test  
0.180467091
0.010615711
0.492569002


$ awk '$8 ~ "ClNonZ" { print $3}' test  
0.180467091
0.010615711
0.492569002

What didn't work(I don't know why and maybe due to my awk version:),

$awk '$8 ~ "^ClNonZ$"{ print $3; }' test
$awk '$8 == "ClNonZ" { print $3 }' test

Thank you all for your answers, comments and help!

2
  • 9
    This has nothing to do with your awk version. You created your test file on Windows so iwhatever tool you used to do that appended control-Ms to the end of each line so the last field on each line is ClNonZ<control-M>, not ClNonZ which is why a RE partial match comparison as done with grep or "~" in awk finds it but an equality comparison doesn't.
    – Ed Morton
    Commented Feb 7, 2013 at 2:21
  • 2
    Yes, make sense. I tried $dos2unix test and then used "==" to replace "~" and it works. Thanks for the explanation! Commented Feb 7, 2013 at 3:12
-3

please try this

echo $VAR | grep ClNonZ | awk '{print $3}';

or

echo cat filename | grep ClNonZ | awk '{print $3}';
1
  • 1
    Sadly this answer does not actually use Awk syntax which the user specifically asked for! Commented Dec 13, 2017 at 11:54

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