2

I have two files : file1 and file2. The content of both files is the following

  • file1: Hello
  • file2: Bla bla bla bla Hello Bla Bla bla bla bla

My objective is to see if I can find an exact match of Hello in file2.


I used the following command: grep -F -f file1 file2. As output I get Bla bla bla bla Hello Bla Bla bla bla bla.

But suppose I change Hello in file1 to just "H" and I run the grep command again ; I also get as output Bla bla bla bla Hello Bla Bla bla bla bla.

What can I do in order to solve that problem and search only for an exact match?

  • 2
    You are getting an exact match. If you want to match a complete word, use -w with grep. – Kusalananda Jul 4 '18 at 14:02
3

You can add those two arguments:

  • -w Matches only word/words instead of substring
  • -o Display only matched pattern instead of whole line

So command will be:

grep -ow -F -f file1 file2

First exemple will output:

Hello

Second one won't output anything since there is no exact match found.

  • The second example would not output anything as there is no H word in the data. – Kusalananda Jul 4 '18 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.