1834208 Members
2637 Online
110066 Solutions
New Discussion

Re: how to 'grep' !!

 
SOLVED
Go to solution
Whitehorse_1
Frequent Advisor

how to 'grep' !!

Admins,

Assume below 'testfile' contains (5 lines),

> cat testfile
apple
apple1
apple2
apple3
apple4
apple5

Qn: Is there anyways I can "grep" only the word "apple". Like,

# grep -? "apple" testfile

I need the output only as "apple".

--WH

Reading is a good course medicine for deep sleep !!
10 REPLIES 10
Ralph Grothe
Honored Contributor
Solution

Re: how to 'grep' !!

Simply use an anchor

e.g.

$ grep 'apple$' testfile
Madness, thy name is system administration
AwadheshPandey
Honored Contributor

Re: how to 'grep' !!

u should use grep -x apple testfile.
see man grep
It's kind of fun to do the impossible
Ralph Grothe
Honored Contributor

Re: how to 'grep' !!

Addendum,
because a string like "apple" might not be the last string of a record in a file one likely more often has some following fields separated by whitespace or some other separator.
In such a case you could use the extended regexp engine of grep and use something similar to this

$ grep -E 'apple[^[:alnum:]]+'

grep -E or egrep can cope with much more intricate filters (see man regexp).
Albeit, the scope of grep is a bit limited and sometimes one needs a sharper tool like Perl which deserved much of its fame from its intrinsic regular expression capabilities.
Madness, thy name is system administration
Whitehorse_1
Frequent Advisor

Re: how to 'grep' !!

Thxs Ralph and Awad..

Qn2: If my file contains,

>cat testfile
one
two
three
four
five

How can I grep the words "one" "three" and "four" using a single "grep" command, like,

#grep -? -? "one" -? "three" -? "four"

--WH
Reading is a good course medicine for deep sleep !!
Oviwan
Honored Contributor

Re: how to 'grep' !!

Hey

$ cat yourfile | grep -E "one|three|four"

see also man regexp

Regards
Ivan Krastev
Honored Contributor

Re: how to 'grep' !!

Use :

cat file| grep -e expr1 -e expr2


grep -e "one" -e "three" -e "four"


regards,
ivan
AwadheshPandey
Honored Contributor

Re: how to 'grep' !!

u can also use a loop for that,
for i in one three four
do
grep $i testfile
done
It's kind of fun to do the impossible
ROCK_10
Advisor

Re: how to 'grep' !!

Hi,

You can just check this command

cat testfile | grep -i apple

"i" makes it case insensitive.



Thanx

Rahul
Dennis Handly
Acclaimed Contributor

Re: how to 'grep' !!

You can of course use grep -w, which is made for what you want.

Contrary to what Rahul, Oviwan and Ivan used, you don't have to use cat with grep. grep takes a list of files.
Hein van den Heuvel
Honored Contributor

Re: how to 'grep' !!

As Ralph corrected himself, the anchor idea is of adequate, but gets mighty combersome for general case where one has to allow for whitespace surrounding it, and begin of line and end of line and what about quotes?

The grep -w option tends to work great, if your grep supports it at all.

Personally I mostly use perl to do my greps and more.
Perl as a "\b" escape for "boundary"

Example:

$ cat > x
apple
apple1
apple2
apple3
snapple
an apple a day
quoted 'apple' test
double-quoted "apple" test
$ perl -ne "print if /\bapple\b/" x
apple
an apple a day
quoted 'apple' test
double-quoted "apple" test

Enjoy.
Hein.