Re: grep command (1126 Views)
Reply
Frequent Advisor
iranzo
Posts: 85
Registered: ‎10-24-2001
Message 1 of 28 (1,124 Views)
Accepted Solution

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
Honored Contributor
Robert-Jan Goossens
Posts: 7,384
Registered: ‎04-04-2000
Message 2 of 28 (1,124 Views)

Re: grep command

Hi,

# grep '$ADRESSIP' file

Robert-Jan
Trusted Contributor
Manish Srivastava
Posts: 170
Registered: ‎05-24-2004
Message 3 of 28 (1,124 Views)

Re: grep command

Hi,

You can do:

grep -w '$ADRESSIP' file

manish.
Honored Contributor
Chris Wilshaw
Posts: 696
Registered: ‎04-09-2002
Message 4 of 28 (1,126 Views)

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
Valued Contributor
Mauro Gatti
Posts: 466
Registered: ‎02-19-2001
Message 5 of 28 (1,126 Views)

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!
Trusted Contributor
Manish Srivastava
Posts: 170
Registered: ‎05-24-2004
Message 6 of 28 (1,126 Views)

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
Frequent Advisor
iranzo
Posts: 85
Registered: ‎10-24-2001
Message 7 of 28 (1,126 Views)

Re: grep command

on my HPUX B.11.00
"grep -x and grep -w" not working !
Problem with patches ?
Bonjour
Honored Contributor
Sanjay Kumar Suri
Posts: 1,063
Registered: ‎12-09-2003
Message 8 of 28 (1,126 Views)

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.
Honored Contributor
Victor Fridyev
Posts: 937
Registered: ‎07-01-1998
Message 9 of 28 (1,126 Views)

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
Honored Contributor
Francisco J. Soler
Posts: 378
Registered: ‎06-15-1999
Message 10 of 28 (1,126 Views)

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.
Honored Contributor
Francisco J. Soler
Posts: 378
Registered: ‎06-15-1999
Message 11 of 28 (1,126 Views)

Re: grep command

Hi again,

I made a mistake in the command, the correct one is:

grep $ADRESSIP file > /dev/null && echo $ADRESSIP.

Sorry
Frank.
Linux?. Yes, of course.
Honored Contributor
Bharat Katkar
Posts: 2,309
Registered: ‎05-29-2003
Message 12 of 28 (1,126 Views)

Re: grep command

Frank,
What about this one:

# cat > xyz
one
two three
four
five
#
# VAR=one
# cat xyz | grep "$VAR"
one
#

Remember i am using default POSIX shell of HPUX.
This should work with KSH also.

Regards,

You need to know a lot to actually know how little you know
Honored Contributor
harry d brown jr
Posts: 8,418
Registered: ‎12-12-2000
Message 13 of 28 (1,126 Views)

Re: grep command

IN RESPONSE TO YOUR: on my HPUX B.11.00 "grep -x and grep -w" not working ! Problem with patches ?


YES, you need to PATCH your system, as "grep -w" and "grep -x" should exist and work.

live free or die
harry
Live Free or Die
Honored Contributor
Bharat Katkar
Posts: 2,309
Registered: ‎05-29-2003
Message 14 of 28 (1,126 Views)

Re: grep command

Sorry,
Earlier one is having bug in that. Try this one. it is bit lengthy, hope you don't mind.

create one script called "myscr" and you have file called "xyz" then:
# cat myscript

#!/usr/bin/ksh
list=`cat xyz`
myfunct()
{
VAR11=$#
while true
do
if [ VAR11 -gt 0 ]
then
echo $1
shift
let VAR11=$VAR11-1
else
exit
fi
done
}
myfunct $list

----- end of script ----------

#chmod +x myscript
#./myscript | grep "$VAR"

I have tested this one and works fine.

Regards,
You need to know a lot to actually know how little you know
Honored Contributor
Bharat Katkar
Posts: 2,309
Registered: ‎05-29-2003
Message 15 of 28 (1,126 Views)

Re: grep command

Now look at this one, more user friendly.
Again don't mind the lenght, it's once for all.

#!/usr/bin/ksh
echo " Enter values to be searched in file : "
read VAR1
echo "\n Enter file name in which the above value to be searched : "
read VAR2
list=`cat $VAR2`
myfunct()
{
VAR11=$#
while true
do
if [ VAR11 -gt 0 ]
then
echo $1
shift
let VAR11=$VAR11-1
else
exit
fi
done
}
myfunct $list | grep "$VAR1"


Let me know if that helped.
Regards,
You need to know a lot to actually know how little you know
Honored Contributor
KapilRaj
Posts: 1,375
Registered: ‎03-07-2000
Message 16 of 28 (1,126 Views)

Re: grep command

Hey,

I get this problem when I use -w. Can u download a gnu version of grep and try grep -w ?.

Regds,

Kaps
Nothing is impossible
Esteemed Contributor
Abdul Rahiman
Posts: 232
Registered: ‎03-23-2004
Message 17 of 28 (1,126 Views)

Re: grep command

I think HP-UX grep don't have the -w option. Someone mentioned about a patch ??

We resolved this be downloading the gnu grep (ggrep) from www.gnu.org. ggrep has much more than -w to offer in my opinion.
No unix, no fun
Honored Contributor
Mark Grant
Posts: 2,712
Registered: ‎04-01-2003
Message 18 of 28 (1,126 Views)

Re: grep command

Does this work

perl -ne '/\b156.152.45.5\b/ && print' datafile
Never preceed any demonstration with anything more predictive than "watch this"
Honored Contributor
Mark Grant
Posts: 2,712
Registered: ‎04-01-2003
Message 19 of 28 (1,126 Views)

Re: grep command

Or, if you want it to take the variable

export ADRESSIP="156.152.45.5"
perl -ne '/\b$ENV{ADRESSIP}\b/ && print'
Never preceed any demonstration with anything more predictive than "watch this"
Honored Contributor
KapilRaj
Posts: 1,375
Registered: ‎03-07-2000
Message 20 of 28 (1,126 Views)

Re: grep command

i can confirm "-w" doesn't work in AIX as well

Kaps
Nothing is impossible
Frequent Advisor
iranzo
Posts: 85
Registered: ‎10-24-2001
Message 21 of 28 (1,126 Views)

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
Honored Contributor
KapilRaj
Posts: 1,375
Registered: ‎03-07-2000
Message 22 of 28 (1,126 Views)

Re: grep command

why don't you ,....

grep "156.152.45.5 toto" file

Kaps
Nothing is impossible
Frequent Advisor
iranzo
Posts: 85
Registered: ‎10-24-2001
Message 23 of 28 (1,126 Views)

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
Honored Contributor
Francisco J. Soler
Posts: 378
Registered: ‎06-15-1999
Message 24 of 28 (1,126 Views)

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.
Honored Contributor
Francisco J. Soler
Posts: 378
Registered: ‎06-15-1999
Message 25 of 28 (1,126 Views)

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.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.