0

I have two strings like below:

Word

More than one word

How to use grep to match only with the first one? I would like to have results when there is no more characters around the word I'm searching for. When there is a string like the second one, it should find nothing.

Is it possible to achieve with grep?

felek
  • 37
  • 1
  • 7

3 Answers3

3

The -x option says the regex needs to match the entire line.

grep -x 'Word' file
tripleee
  • 175,061
  • 34
  • 275
  • 318
1

Use this expression

grep -i ^word$ file

Explanation:

Find all lines starting with (^) at the end of the line ($).
The -i flag makes the match insensitive, remove it if you want a case sensitive match

Francesco Gasparetto
  • 1,819
  • 16
  • 20
0

Match the entire line:

grep -E '^Word$'

or

grep -P '^Word$'

Use -i to make it case insensitive, -w to only match entire words only.

zmb
  • 61
  • 5