macha
macha

Reputation: 7487

Get the matches only if the substring is not part of a string

Hello I actually am looking for a string which is a substring of another string. So I am using grep to get the matches of this string, but the matches of the other string too are coming up.

grep -nr 'XML' .

when I do this, the matches for string "LIBXMLX" are also coming up. Is there a way to get matches only of XML and no LIBXMLX??

I am newbie to shell scripting, so how do I proceed with this?

Upvotes: 0

Views: 604

Answers (3)

glenn jackman
glenn jackman

Reputation: 246799

From the manual

The symbols \< and \> respectively match the empty string at the beginning and end of a word.

So,

grep -nr '\<XML\>' .

Upvotes: 3

PROGRAM_IX
PROGRAM_IX

Reputation: 424

It's kludgey, I know, but you could use ' XML' as the search string, thereby eliminating anything that doesn't have a space before the X. Just a thought. My interactions with grep are simple.

Upvotes: 0

Karoly Horvath
Karoly Horvath

Reputation: 96258

Use the -w switch (exact word match).

Upvotes: 0

Related Questions