0

I want to get the lines that contain a defined word with grep.

Edit: The solution was this one.

I know that I can use the -w option, but it doesn't seems to do the trick.

For example: every word that contains my defined word separated by punctuation signs is included. If I look for dogs, it will show me lines that contain not only dogs word but also cats.dogs, cats-dogs, etc.

# cat file.txt
some alphadogs dance
some cats-dogs play
none dogs dance
few dog sing
all cats.dogs shout

And with grep:

# cat file.txt | grep -w "dogs"
some cats-dogs play
none dogs dance
all cats.dogs shout

Desired output:

# cat file.txt | grep -w "dogs"
none dogs dance

Do you know any workaround that allows you to get the whole word? I've tested it with \b or \< with negative results.

Thanks, Eudald

Community
  • 1
  • 1
eudald_v
  • 1
  • 1
  • 3
  • Show expected output – 123 Apr 11 '17 at 10:12
  • 2
    `grep -w` would obviously not work here @triplee, it cannot be duplicated to the current question – Inian Apr 11 '17 at 10:19
  • 1
    `grep -w` fails because punctuation and end/start of line character are viewed as non-word characters; also even if it did work the implementation can be very slow – Chris_Rands Apr 11 '17 at 10:21
  • 2
    @lnian If you can find a better duplicate, I'll be happy to link to that. It's not like this is a terribly original question. The currently designated duplicate does have answers which suggest anchors, though I agree that a better duplicate would be nice. – tripleee Apr 11 '17 at 10:23
  • 2
    @tripleee maybe this http://stackoverflow.com/questions/4709912/how-to-make-grep-only-match-if-the-entire-line-matches – Chris_Rands Apr 11 '17 at 10:25
  • Hello @tripleee , the existent problem is that the word exist in a string context, so I can't anchor neither beginning or end of line. I'm going to edit the question to make it more clear, sorry. – eudald_v Apr 11 '17 at 14:20

2 Answers2

1

Use the word-boundary anchors, in any version of grep you have installed

grep '^dogs$' file.txt

An excerpt from this regular-expressions page,

Anchors

[..] Anchors do not match any characters. They match a position. ^ matches at the start of the string, and $ matches at the end of the string.[..]

Community
  • 1
  • 1
Inian
  • 80,270
  • 14
  • 142
  • 161
  • Have you even tested it with the examples I provided? It doesn't work – eudald_v May 08 '17 at 10:57
  • @eudald_v: You didn't let us know with you sample updates, refer the timestamps to see. Not a problem, can you try `grep '[[:space:]]dogs[[:space:]]' file` which should solve your problem. let us know if it did – Inian May 08 '17 at 11:06
  • it only solves the example if the string has a space before, which in the sample happens but could not happen if the string is at the beginning or end of the sentence. Anyway, I edited the question again with the answer I finally found that works. – eudald_v May 08 '17 at 14:03
  • @eudald_v: Please understand, we can only test with the inputs you have provided. When you say it did;t work for cases other than having spaces, but you did not let us know such a input in your example. – Inian May 08 '17 at 14:05
-1

Try with -x parameter:

grep -x dogs file.txt

From grep manual:

-x, --line-regexp

Select only those matches that exactly match the whole line. (-x is specified by POSIX.)

NOTE: cat is useless when you pipe its output to grep

Zumo de Vidrio
  • 2,021
  • 2
  • 15
  • 33