Home Linux Community Forums Software Command Line using wget,, and sed

using wget,, and sed

Link to this post 24 Feb 11

I am trying to get my external Ip using cygwin on a Windows 7 box. I am almost there but I am having an issue with the sed substitution syntax. As you see, I am getting the IP, but I don't want the </body> part of the output.

C:\>wget -q -O index1.html && sed "s/.*:\(.*\)<.*/\1/" index1.html</body>

Without the -q switch the output is:

<html><head><title>Current IP Check</title></head><body>Current IP Address:</body></html>

How do I have to change the sed command? Instead of creating the index1.html file, I also tried unsuccessfully to use pipe and grep, but failed to get that working properly. Alternative approaches would also be appreciated, but I would like to know what I am doing wrong with the sed commands. I have tried many different variations, but just can't seem to get just the IP address.

Thank you,

Link to this post 26 Feb 11

I am not sure if SED is the string manipulator to use for this kind of outputs.

I used cut and Below command could work for you..

wget -q -O index.html && cat index.html | cut -d '>' -f 7 | cut -d '<' -f 1


Link to this post 26 Feb 11

or with awk it could look like this :)

wget -q -O index2.html && awk '{print $6}' index2.html | awk -F'<' '{print $1}'

Link to this post 26 Feb 11

Hi, Karma

The "cut" and "awk" commands used in your examples produced slightly different results, but each example was most helpful in illustrating the basics of these two commands. I now have two command options for filtering output/text.

Cut produces: Current IP Address:

AWK produces:

Thank you kindly,

Link to this post 28 Feb 11

below is for getting only IP using CUT

wget -q -O index.html && cat index.html|cut -d ' ' -f 6 | cut -d '<' -f 1

Link to this post 13 Mar 11

You could also skip the intermediate file by using curl instead of wget, which is a command whose main use is to pipe a web page as a text stream to some other command. Some systems come without curl preinstalled, but it's quite common (and useful) in scripts, so in that case I recommend installing it.

curl -s | sed 's/.*<body>Current IP Address: //;s/<\\/body>.*//'

Note the semicolon used in the argument to sed; it's used to separate different expressions when you want to do multiple substitutions without calling sed multiple times.

Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board