how to grep ip address ?

hi,

Pls advice me, how to grep only the ip address from the following output:

<ip>10.4.65.67</ip>
<TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA>false</TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA>
<TLS_RSA_EXPORT1024_WITH_RC4_56_SHA>false</TLS_RSA_EXPORT1024_WITH_RC4_56_SHA>
<ip>10.4.65.67</ip>
<TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA>false</TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA>
<TLS_RSA_EXPORT1024_WITH_RC4_56_SHA>false</TLS_RSA_EXPORT1024_WITH_RC4_56_SHA>
<ip>10.4.65.172</ip>
<TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA>false</TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA>
<TLS_RSA_EXPORT1024_WITH_RC4_56_SHA>false</TLS_RSA_EXPORT1024_WITH_RC4_56_SHA>
<ip>10.4.65.46</ip>
<TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA>false</TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA>
<TLS_RSA_EXPORT1024_WITH_RC4_56_SHA>false</TLS_RSA_EXPORT1024_WITH_RC4_56_SHA>
<ip>10.4.65.46</ip>
<ip>10.4.65.237</ip>
<TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA>false</TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA>
<TLS_RSA_EXPORT1024_WITH_RC4_56_SHA>false</TLS_RSA_EXPORT1024_WITH_RC4_56_SHA>
<ip>10.4.65.79</ip>
<TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA>false</TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA>
<TLS_RSA_EXPORT1024_WITH_RC4_56_SHA>false</TLS_RSA_EXPORT1024_WITH_RC4_56_SHA>
<ip>10.4.65.79</ip>

for example:
I want the output as:
10.4.65.67
10.4.65.67
10.4.65.172
10.4.65.46
10.4.65.46
10.4.65.237
10.4.65.79
10.4.65.79

sed -n "s+<ip>\(.*\)<.*+\1+p" file1 
10.4.65.67
10.4.65.67
10.4.65.172
10.4.65.46
10.4.65.46
10.4.65.237
10.4.65.79
10.4.65.79

Or if your awk support this:

awk -F"[<>]" '/ip/ { print $3 }' file1 
10.4.65.67
10.4.65.67
10.4.65.172
10.4.65.46
10.4.65.46
10.4.65.237
10.4.65.79
10.4.65.79

or
awk -F"</*ip>" '/ip/ { print $2 }' file1

try...

cat file | grep "<ip>" | awk -F "[<ip></ip>]" '{print $5}'

The solution of scottn is better....i have to learn a lot....

Hi
Try this

grep "ip" <filename>|cut -f2 -d"<"|cut -f2 -d">"

Regards,
Pankaj

---------- Post updated at 07:27 AM ---------- Previous update was at 07:17 AM ----------

Hi Scottn,

May i request you to explain the command u have send

awk -F"[<>]" '/ip/ { print $3 }' filename - why "/ip/" need to mentione over here?

sed -n "s+<ip>\(.*\)<.*+\1+p" filename - i could not understand... :frowning:

Regards,
Pankaj

grep -e '^[1-9][0-9]\.[1-9][0-9]\.[1-9][0-9][]0-9]\.[1-9][0-9][]0-9]' file

If awk supports it you can specify more than one field separator (-F"[<>]") or field separators with more that one character or with regular expressions ("</*ip>"). And I only want lines with ip in them (/ip/)

Better maybe would have been

$2 == "ip" { print $3 }

remember everything after "<ip>" ( \(.*\) ) and before the next (actually the last) "<", and then print it "...\1p"
I don't know what "better is" here, because my regex's are rubbish!

OK if you want the whole line with the IP address in it. And I can't see a dot (.) on any other line, so based on the input file

grep \\. file

would have done the same thing.


  1. 1-9 ↩︎

awk '/ip/{gsub(/<ip>|<\/ip>/,"");print}' file