1752717 Members
5608 Online
108789 Solutions
New Discussion юеВ

grep command

 
SOLVED
Go to solution
iranzo
Frequent Advisor

Re: grep command

Thanks to all
OK, "grep -x" works !
but the reel file is :
156.152.45.5 toto
156.152.45.58 titi
156.152.45.589 bubu
And that i want is only the field "toto"
of the file when $ADRESSIP=156.152.45.5


Bonjour
KapilRaj
Honored Contributor

Re: grep command

why don't you ,....

grep "156.152.45.5 toto" file

Kaps
Nothing is impossible
iranzo
Frequent Advisor

Re: grep command

Because, I don' know that the text is 'toto'.
This is en example.
I search a comment by the variable $ADRESSIP.

This is not so easy...
Bonjour
Francisco J. Soler
Honored Contributor

Re: grep command

Hi Iranzo,
if the name "toto" or whatever is the second field, you can do:

awk '/'$ADRESSIP' / {print $2}' file

This prints only the second field if the first one matches $ADRESSIP. Be carefully with the space after '/'$ADRESSIP' /
^ this one

Hope this helps.

Frank.
Linux?. Yes, of course.
Francisco J. Soler
Honored Contributor

Re: grep command

Hi,

Sorry but the space is not clear enought in the previus message,

awk '/'$ADRESSIP'/ {print $2}' file

I think this is "more" clear.

Frank
Linux?. Yes, of course.
RAC_1
Honored Contributor

Re: grep command

I think this is not possible with grep/egrep/fgrep. May be gnu grep will help.

Without gnu grep, you can do it as follows.

grep "156.152.45.5" ./file|awk -F " " '{if(length($1)==12)print}'

Anil
There is no substitute to HARDWORK
curt larson_1
Honored Contributor

Re: grep command

similar to Mr. Soler's

awk -v var=$ADRESSIP '
$1 == var {print $2;}' yourFile
Muthukumar_5
Honored Contributor

Re: grep command

Hai,

We can use ^ and $ to notify the begin and end of string. It is easy to get the exact string.

ADRESSIP=156.152.45.5
grep ^${ADRESSIP}$

Regards,
Muthukumar
Easy to suggest when don't know about the problem!