Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-25-2008, 05:37 PM   #1
Registered: Jan 2007
Location: Athlone, ROI
Distribution: Ubuntu Hardy Desktop, Solaris 10, Workstation 2008 x64
Posts: 75

Rep: Reputation: 16
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 )

Thanks guys

Last edited by Geneset; 08-25-2008 at 05:38 PM. Reason: accidental smilies
Old 08-25-2008, 08:36 PM   #2
Mr. C.
Senior Member
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
I think I understand that you saying you have something like: TCP

and you want them to be:

hostname1:1000 hostname2:2000

If so, here you go:
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.

Last edited by Mr. C.; 08-25-2008 at 08:37 PM.
Old 08-25-2008, 10:55 PM   #3
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
awk 'BEGIN{}
 cmd = "dig -x "src[1]" +noall +answer +short"
 cmd | getline h
 cmd = "dig -x "dest[1]" +noall +answer +short"
 cmd | getline d
 print "host: "h":"src[2]" host: "d":"dest[2] 
' file
Old 08-26-2008, 04:59 AM   #4
Registered: Jan 2007
Location: Athlone, ROI
Distribution: Ubuntu Hardy Desktop, Solaris 10, Workstation 2008 x64
Posts: 75

Original Poster
Rep: Reputation: 16
And thats why i keep coming back here, thanks guys


awk, hostname, ip

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
deleting a field using awk jkeertir Linux - Newbie 5 04-13-2008 10:55 PM
My field separator changes when using awk Helene Programming 3 05-01-2004 08:10 AM
Two field seperators in awk?? Astro Programming 2 11-09-2003 10:12 AM
How do I run an awk script? davee Programming 2 08-12-2003 08:46 AM
How do I zip and attach the output data of a grep | awk | mail shell script? 360 Programming 1 05-08-2002 08:26 AM > Forums > Non-*NIX Forums > Programming

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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration