awk match whole word

Solution 1:

If you want to pass "ABC" as a variable instead of hardcoding it, use the matching operator:

awk -v word=ABC '$0 ~ "(^|[^[:alpha:]])" word "([^[:alpha:]]|$)"'

With gawk (other awks too?) you can use \< and \> to denote word boundaries, where a word is a sequence of letters, digits and underscore (I believe), so this will work for your example:

awk '/\<ABC\>/'

Solution 2:

Use \y for word boundary, e.g.

awk '/\yABC\y/'

See https://www.gnu.org/software/gawk/manual/html_node/GNU-Regexp-Operators.html for more details.

Solution 3:

Figured it out - was having problems due to a typo

awk '/[^[:alpha:]]ABC[^[:alpha:]]/'