I want to add word boundaries to this awk command:
awk '{$0=tolower($0)};/wordA/&&/wordB/ { print FILENAME ":" $0; }' myfile.txt
I tried adding \y
at left and right of wordA
and wordB
but it didn't work in my tests.
I tried this: /\ywordA\y/&&/\ywordB\y/
Thanks all!
(ps: I'm new to awk so I was trying to avoid the match() function.)
This might work for you on Mac OS X:
But as it won't work on linux you're best off installing GNU awk.
You want to use gawk instead of awk:
will do what you want, if your system has gawk (e.g. on Mac OS X). \y is a GNU extension to awk.
\<
and\>
conventions for word boundaries.If you're stuck with a recalcitrant awk, then since awk is normally splitting lines into tokens anyway, it might make sense to use:
function word(s, i) { for (i=1;i<=NF;i++) {if ($i ~ "^" s "$") {return i}}; return 0; }
So, for example, instead of writing
you could just as easily write: