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,190
  • 1
  • 9
  • 17

2 Answers2

0

Try to use word boundaries in grep:

grep "\<bc\>" file
anubhava
  • 713,503
  • 59
  • 514
  • 593
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
  • 223,850
  • 36
  • 205
  • 328