Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > Format command line output?

Format command line output?
Thread Tools
Mac Enthusiast
Join Date: Apr 2001
Location: Camarillo, CA
Status: Offline
Reply With Quote
Oct 17, 2003, 12:46 PM
 
I'm attempting to use GeekTool to display rendezviews information on my desktop (pertaining to itunes music sharing), how that people at my work have the ability to access my music...

I'm using a netstat command and filtering it with various commands to narrow down the output. The command, as I've got it now, is:

netstat | grep 3689 | tr -s " " "," | cut -f5 -d','

The result is something looking symiler to:

10.11.1.2.2885
10.11.1.3.2885

I'm trying to further refine this in a couple of manners. First is I want to strip the port number off the end. While I can use the cut command to split by the . and then choose the first 4 intervals, I cannot get them to print with the periods intact. (ie: I get 101112). I've been playing with using other commands, but nothing I've tried gives me the right result.

Anybody know how to this??? Keep in mind that the command needs to be in a single line.
Also keep in mind that this can end up being multiple occurences (each on it's own line).

I also wanted to find out if there is a way to add a prefix in front of the out put. I plan on cutting the lines so they all end up on the same line, but I would like to add a single prefix to the front, like:
Connected Systems: 10.11.3.2.2285

I also would like to replace the line returns with a comma and a space. I can figure out how to do it with just a comma or a space, but not both...

So basically the end result will look like:

Connected Sytems: 10.11.1.2, 10.11.1.3


Can anybody help???
     
Professional Poster
Join Date: Oct 1999
Location: :ИOITAↃO⅃
Status: Offline
Reply With Quote
Oct 17, 2003, 01:29 PM
 
awk/sed:

Code:
netstat -f inet -W | awk '$4 ~ /3689/ {print "iTunes connection from: "$5}' | sed -e 's/\.[^.]*$//g'
or perl, which better matches your output requirements:
Code:
echo "Connections from: " ; netstat -f inet -W | perl -ne '@fields = split(/\s+/); if ($fields[3] =~ /3689/) { ($host) = $fields[4] =~ /([\d\.]+)\.\d+/; print "$host, "; }'
     
Bobby  (op)
Mac Enthusiast
Join Date: Apr 2001
Location: Camarillo, CA
Status: Offline
Reply With Quote
Oct 17, 2003, 02:15 PM
 
Awesome! Thank you!!!

Originally posted by Mithras:
awk/sed:

Code:
netstat -f inet -W | awk '$4 ~ /3689/ {print "iTunes connection from: "$5}' | sed -e 's/\.[^.]*$//g'
or perl, which better matches your output requirements:
Code:
echo "Connections from: " ; netstat -f inet -W | perl -ne '@fields = split(/\s+/); if ($fields[3] =~ /3689/) { ($host) = $fields[4] =~ /([\d\.]+)\.\d+/; print "$host, "; }'
     
Bobby  (op)
Mac Enthusiast
Join Date: Apr 2001
Location: Camarillo, CA
Status: Offline
Reply With Quote
Oct 17, 2003, 05:13 PM
 
Having known perl and not getting *exactly* what I wanted here, I fixed the perl to give me what I wanted... I couldn't have done this without knowing the other commands though...


netstat -f inet -W | perl -ne ' @fields = split(/\s+/); if ($fields[3] =~ /3689/) { ($host) = $fields[4] =~ /([\d\.]+)\.\d+/; if( $result != "1" ) { print "Connected iTunes Users: "; $result='1'; } else { print ", "; } print "$host" }'

Gives me:


Connected iTunes Users: x.x.x.x, x.x.x.x, x.x.x.x

Thanks again...
     
   
Thread Tools
Forum Links
Forum 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 04:02 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2