1

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?

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

1 Answer 1

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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .