1827854 Members
1495 Online
109969 Solutions
New Discussion

help on grep command

 
SOLVED
Go to solution
Alan Buenaventura
Occasional Contributor

help on grep command

Is there an option for grep on hpux to search for (just) a word? Suppose a file contains the ff.

line 1:abc def gef
line 2:ab cdef hig

How could grep search for the word "ab" only (on line 2) and ignore "abc" (on line 1)?

Thanks.
5 REPLIES 5
Vikas Khator
Honored Contributor
Solution

Re: help on grep command

Hi ,

egrep -e " ab |^ab | ab$"

Note that first condition has blank space before ab and after ab .

Second condition has a blank space at end .

Third condition has blank space in front.

Hope this helps.
Keep it simple
Patrick Wallek
Honored Contributor

Re: help on grep command

I think I saw on a previous post that there is a '-x' option to grep that lets you grep for exact matches.

So the following:

grep -x ab testfile
Bruce Regittko_1
Esteemed Contributor

Re: help on grep command

Hi,

According to the man page for grep(1), the -x option matches the entire line, not a whitespace delimited word. Sorry,

--Bruce
www.stratech.com/training
Dan Hetzel
Honored Contributor

Re: help on grep command

Hi Alan,

Victor's suggestion is good, but a more general approach would be with using regular expressions character classes:
(see man 5 regexp for details)

echo "ab c def ghi" | grep -e "[[:space:]]ab[[:space:]]|^ab[[:space:]]|[[:space:]]ab$"

The [:space:] character class matches all characters producing white space on the displayed text

Best regards,

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Joseph C. Denman
Honored Contributor

Re: help on grep command

grep 'ab ' filename
If I had only read the instructions first??