1828216 Members
2218 Online
109975 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.
Francisco J. Soler
Honored Contributor

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.
Bharat Katkar
Honored Contributor

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
harry d brown jr
Honored Contributor

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
Bharat Katkar
Honored Contributor

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
Bharat Katkar
Honored Contributor
Solution

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
KapilRaj
Honored Contributor

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
Abdul Rahiman
Esteemed Contributor

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
Mark Grant
Honored Contributor

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"
Mark Grant
Honored Contributor

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"
KapilRaj
Honored Contributor

Re: grep command

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

Kaps
Nothing is impossible
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.