hendry
hendry

Reputation: 10813

Awk print matching exact column contents

echo -e "55 11\n25 11.0" | awk '$2 ~ /11/{print $1}'

I only want to match "11", and not "11.0"'s value 25. Any tips?

Upvotes: 2

Views: 5301

Answers (3)

jaypal singh
jaypal singh

Reputation: 77085

Ensure last field does not contain a decimal point.

[jaypal:~/Temp] echo -e "55 11\n25 11.0" | awk '$NF!~/\./{print $1}'
55

Upvotes: 0

h.j.k.
h.j.k.

Reputation: 1407

echo -e "55 11\n25 11.0" | awk '$2 ~ /^11$/ {print $1}'

Upvotes: 0

johnsyweb
johnsyweb

Reputation: 141780

Match the whole field:

% echo -e "55 11\n25 11.0" | awk '$2 ~ /^11$/{print $1}'
55

If you did want to match numerically, you should not use a regular expression, of course (your current one would also match 6119.42):

% echo -e "55 11\n25 11.0" | awk '$2 == 11 { print $1 }'
55
25

Upvotes: 5

Related Questions