LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   awk run script on field before output (http://www.linuxquestions.org/questions/programming-9/awk-run-script-on-field-before-output-665348/)

Geneset 08-25-2008 05:37 PM

awk run script on field before output
 
hi guys,
quick question

i have an input that is ipaddress: port ipaddress: port TCP/UDP
can i take the ip's and "host ip" them on output?

I'm looking at something like

awk -F" |:" '{ print 'host $1'":"$2, 'host $3'":"$4,$5}' or something along those lines that will do in line substitution of these values for their hostnames

(replace "host ip" with "host ip | cut -d' ' -f5", but thats not as tidy in the above :D )

Thanks guys

Mr. C. 08-25-2008 08:36 PM

I think I understand that you saying you have something like:

192.168.0.1:1000 10.0.0.1:2000 TCP

and you want them to be:

hostname1:1000 hostname2:2000

If so, here you go:
Code:

perl -F'/[:\s]/' -nae '@names = map { qx/dig -x $_ +noall +answer +short/ } @F[0,2]; chomp @names; print "host $names[0] : $F[1] host $names[1] : $F[3] $F[4]\n";'
Provide your input file as the last argument, or send it through a pipe. Adjust the print to your needs.

ghostdog74 08-25-2008 10:55 PM

Code:

awk 'BEGIN{}
{
 split($1,src,":")
 split($2,dest,":")
 cmd = "dig -x "src[1]" +noall +answer +short"
 cmd | getline h
 gsub(/\.$/,"",h)
 cmd = "dig -x "dest[1]" +noall +answer +short"
 cmd | getline d
 gsub(/\.$/,"",d)
 print "host: "h":"src[2]" host: "d":"dest[2]
}
' file


Geneset 08-26-2008 04:59 AM

And thats why i keep coming back here, thanks guys


All times are GMT -5. The time now is 11:41 PM.