0

I want to only grep for a whole word. The problem is a file contains non-english characters, so grep -w doesn't work (f.e. matches "aąbcć" when searching for "bc"). I can't write any working regex with lookaround either. Can anybody help me?

kszl
  • 1,203
  • 1
  • 11
  • 18

2 Answers2

0

Try to use word boundaries in grep:

grep "\<bc\>" file
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

Requiring GNU grep: grep -P '(^|\s)\Kbc(?=$|\s)' file

Using awk, I wonder if this would work:

awk -v word="bc" '{for (i=1; i<=NF; i++) if ($i == word) {print; break}}' file
glenn jackman
  • 238,783
  • 38
  • 220
  • 352