2

I have a file with a list of word and I want to identify only the word in the file which exactly matches another word?

So, for example, if I have in the file, the words "BEBE, BEBÉ, BEBÉS", and I look for "BEBE", I want it to return just the first one, which is the exact match.

I tried using grep -w "BEBE" filename.txt, but it doesn't work. It still gives me back all three of them.

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
user1835630
  • 251
  • 2
  • 6
  • 17
  • 1
    Is the problem that it's matching all three on the line? Or that it is returning multiple matching lines in the same file? – Andy Lester Dec 10 '12 at 15:11
  • show us exactly what the input file looks like, and exactly what you're expecting as output, and this should be trivial to solve. – nullrevolution Dec 12 '12 at 19:33

1 Answers1

2

Use -o to only display the part that matches with -w, also use -F for fixed string if you're not regex matching:

$ cat file
BEBE, BEBÉ, BEBÉS

$ grep -woF 'BEBÉ' file
BEBÉ

$ grep -woF 'BEBÉS' file
BEBÉS
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
  • 1
    No, this doesn't work. It retunrs "BEBE BEBE BEBE" and I also need the rest of the line after the matching word, and in this case, the line is deleted. – user1835630 Dec 10 '12 at 15:05
  • What does `locale` and `file file` produce `file: UTF-8 Unicode text`? This [question](http://stackoverflow.com/questions/4739480/grep-regex-cant-find-accented-word) might be of use – Chris Seymour Dec 10 '12 at 15:08