awk equivalent of regex

Hi all,

Can someone tell me what's the (g)awk equal of this simple regex to find ip addresses in urls:

egrep "^http://[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(:[0-9]{1,5})?/"

Input:

http://10.0.0.1/query.exe
http://11y10x09w:80/howaboutme
http://192.168.100.190:1234/takeme.gpg

Output:

http://10.0.0.1/query.exe
http://192.168.100.190:1234/takeme.gpg

I've been breaking my head over the last couple of hours trying to figure out how to define range in awk like this. {1,3} :wall:

awk 'match($0,"http://[0-9]+.[0-9]+.[0-9]+.[0-9]+.")' infile

--ahamed

Thanks for the reply. The code doesn't work properly:

http://12345.678910.11121314.com/howaboutme

That is technically not an IP [ valid or not ], but your script accepts it.

hi.. try this ...

input - filename is f7

 
http://10.0.0.1/query.exe
http://11y10x09w:80/howaboutme
http://192.168.100.190:1234/takeme.gpg
http://1923.168.100.190:1234/takeme.gpg

script

 
awk '{ if($0 ~ /^http:\/\/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(:[0-9]{1,5})?/) print $0}' f7

output

http://10.0.0.1/query.exe
http://192.168.100.190:1234/takeme.gpg

Regards,
A!

Hi A!,

For some reason, your code doesn't give me any output on my machine. I'm running Ubuntu 10.04 with GNU Awk 3.1.6 :confused:

From gawk documentation :

Try :

awk '{ if($0 ~ /^http:\/\/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]{1,3}\.[0-9]\{1,3\}(:[0-9]\{1,5\})?/) print $0}' f7

I test the original awk command with some versions of awk :

  • Solaris /usr/bin/awk : No output
  • Solaris /usr/bin/nawk : No output
  • Solaris /usr/xpg4/bin/awk : Ok.
  • Aix : /usr/bin/awk : Ok.

Jean-Pierre.

2 Likes

Thanks Jean.

This code worked :

awk --posix '/^http:\/\/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(:[0-9]{1,5})?\// { print $0 }' filename

Apologies for not RTFM properly :o

Same code you can shorten it to

awk --posix '/^http:\/\/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(:[0-9]{1,5})?/' infile

--ahamed

And further to:

awk '/^http:\/\/[0-9]{1,3}(\.[0-9]{1,3}){3}(:[0-9]{1,5})?\//'
1 Like