LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 08-25-2008, 05:37 PM   #1
Geneset
Member
 
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: 59
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.

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

Rep: Reputation: 241Reputation: 241Reputation: 241
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
 
Old 08-26-2008, 04:59 AM   #4
Geneset
Member
 
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
 
  


Reply

Tags
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


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

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