1753772 Members
5302 Online
108799 Solutions
New Discussion юеВ

Re: grep command

 
SOLVED
Go to solution
iranzo
Frequent Advisor

grep command

Hello,
here my file: 156.152.45.5
156.152.45.58
156.152.45.589
and variable $ADRESSIP=156.152.45.5
i want to do a "grep" $ADRESSIP on my file
with only $ADRESSIP in the output and not
other word (not 156.152.45.58,156.152.45.589)
Thanks a lot.
Bonjour
27 REPLIES 27
Robert-Jan Goossens
Honored Contributor

Re: grep command

Hi,

# grep '$ADRESSIP' file

Robert-Jan
Manish Srivastava
Trusted Contributor

Re: grep command

Hi,

You can do:

grep -w '$ADRESSIP' file

manish.
Chris Wilshaw
Honored Contributor

Re: grep command

Try

grep "$ADRESSIP$" file

Note the $ at the end of the ADDRESSIP. This means that you will get any line that contains your IP address, followed by an end of line marker. This should exclude the other addresses from your search.

If that doesn't work, it may be that you have a space or tab character following the IP address. In this case, use

grep "$ADRESSIP " file
or
grep "$ADRESSIP" file

(use the TAB key rather than typing
Mauro Gatti
Valued Contributor

Re: grep command

Did you try:
grep -x $ADRESSIP file?
Using -x (eXact) Matches are recognized only when the entire input line matches the fixed string or regular expression.

RGDS


Mauro
Ubi maior, minor cessat!
Manish Srivastava
Trusted Contributor

Re: grep command

Hi,

It works for me:

$cat file
debug
debug1
$DF=debug
$grep -w $DF file
debug


As you can see debug1 is not displayed. It displays only the word which we are looking for.

manish
iranzo
Frequent Advisor

Re: grep command

on my HPUX B.11.00
"grep -x and grep -w" not working !
Problem with patches ?
Bonjour
Sanjay Kumar Suri
Honored Contributor

Re: grep command

Check with egrep

$egrep -w $ADRESSIP file_name
$egrep -x $ADRESSIP file_name

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Victor Fridyev
Honored Contributor

Re: grep command

Hi,

If you don't hate awk 8))):

awk `$1==A {print }' A=$ADDRESSIP $FILE

HTH
Entities are not to be multiplied beyond necessity - RTFM
Francisco J. Soler
Honored Contributor

Re: grep command

Hi,

If you only need $ADRESSIP in the output, you can do a grep and if grep is successful then print the ip, for example:

ADRESSIP=156.152.45.5
grep $ADRESSIP file && echo $ADRESSIP

if no $ADRESSIP in file the command line output nothing, if $ADRESSIP in file the command line outputs the $ADRESSIP only

Frank.
Linux?. Yes, of course.