awk regex doesn't work when match ip address

I wanna extract ip address in a file, each line of the file is like:

T   218.241.107.98  167.232.255.245 7   2719    1378473670  N   0   0   0   G   0   I   218.241.107.97,0.146,1  218.241.98.45,0.239,1   192.168.1.253,0.182,1   159.226.253.77,0.210,1  159.226.253.54,0.676,1  159.226.254.254,39.287,1    203.192.137.173,39.335,1    203.192.134.69,50.128,1 61.14.157.141,42.917,1  202.147.61.193,188.165,1    38.104.84.41,201.100,1  154.54.30.193,194.939,1 154.54.41.221,194.915,1 154.54.5.65,237.396,1   154.54.2.81,251.547,1   154.54.24.153,260.946,1 154.54.26.126,256.046,1 154.54.10.14,245.145,1  193.251.240.113,241.663,1   q   q   q   57.69.31.22,283.784,1;57.69.31.22,284.763,1

But my awk script doesn't work

#!/usr/bin/awk -f
BEGIN {
    FS = "[, \t;]"
}
{
    for(i = 4; i <= NF; i++) 
    {
        if ($1 == "#")
            continue
        if ($i ~ /(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}/)
            printf $i"\t"
        if (i == NF) 
            printf "\n"
    }
}

Can anyone figure out what's wrong?

Any help will be really appreaciated, thanks in advance.

PS: there is no output but a new line character

Answers


Try this awk

awk -F"[, \t;]+" '!/^#/ {for (i=1;i<NF;i++) if ($i ~ /(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}/) printf "%s\t",$i;print ""}' file
218.241.107.98  167.232.255.245 218.241.107.97  218.241.98.45   192.168.1.253   159.226.253.77  159.226.253.54  159.226.254.254 203.192.137.173 203.192.134.69  61.14.157.141   202.147.61.193  38.104.84.41        154.54.30.193   154.54.41.221   154.54.5.65     154.54.2.81     154.54.24.153   154.54.26.126   154.54.10.14    193.251.240.113 57.69.31.22     57.69.31.22

This !/^#/ makes it only prints line not starting with #


Need Your Help

Ofbiz service creation

java ofbiz

Starting to learn ofbiz, I am following the tutorial here:

Passing stdout colored output to grunt output

javascript node.js gruntjs

I'm trying to create a grunt custom task that runs mocha tests but I can't figure out how to have grunt take the colored output from mocha and display it as it does when running the mocha command

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.