Fredifqh
Fredifqh

Reputation: 87

Exact string match in awk

I have a file test.txt with the next lines

1997           100   500   2010TJ
2010TJXML      16    20    59

I'm using the next awk line to get information only about string 2010TJ

awk -v var="2010TJ" '$0 ~ var {print $0}' test.txt

But the code print the two lines. I want to know how to get the line containing the exact string

1997  100   500   2010TJ

the string can be placed in any column of the file.

Upvotes: 0

Views: 16259

Answers (3)

dawg
dawg

Reputation: 103744

Several options:

Use a gawk word boundary (not POSIX awk...):

$ gawk '/\<2010TJ\>/' file

An actual space or tab or what is separating the columns:

$ awk '/^2010TJ /' file

Or compare the field directly to the string:

$ awk '$1=="2010TJ"' file

You can loop over the fields to test each field if you wish:

$ awk '{for (i=1;i<=NF;i++) if ($i=="2010TJ") {print; next}}' file

Or, given your example of setting a variable, those same using a variable:

$ gawk -v s=2010TJ '$0~"\\<" s "\\>"' 
$ awk -v s=2010TJ '$0~"^" s " "'
$ awk -v s=2010TJ '$1==s'

Note the first is a little different than the second and third. The first is the standalone string 2010TJ anywhere in $0; the second and third is a string that starts with that string.

Upvotes: 6

karakfa
karakfa

Reputation: 67467

another awk with word boundary

awk '/\y2010TJ\y/' file

note \y matches either beginning or end of a word.

Upvotes: 0

Gilles Qu&#233;not
Gilles Qu&#233;not

Reputation: 184995

Try this (for testing only column 1) :

awk '$1 == "2010TJ" {print $0}' test.txt

or grep like (all columns) :

gawk '/\<2010TJ\>/ {print $0}' test.txt

Note

\< \> is word boundarys

Upvotes: 0

Related Questions