1748259 Members
3929 Online
108760 Solutions
New Discussion юеВ

Re: Sort by port range

 
allanm77
Frequent Advisor

Sort by port range

Hi All,JRF

Have the following command and its output -

/usr/bin/curl http://URL |grep VIP |awk '{print $1,$3}'|/usr/bin/perl -lne 'm{"(.*?)".*?//(.*):(\d+)/} and print "$2 $3"'|sort -u -k2,2

VIP 2001
VIP 3001
VIP 4001
VIP 4233
VIP 3002
VIP 2020
тАж..

Need to have an output in a way so that I get only 2xxx port series or 3xxxx or 4xxxx and not all. Can we trim the output in this fashion?

Desired output can be (if sorting by ports of 2xxx series only) -

VIP 2001
VIP 2020
...

Thanks,
Allan.

6 REPLIES 6
Dennis Handly
Acclaimed Contributor

Re: Sort by port range

>Can we trim the output in this fashion?

 

You have several places in your pipeline where you can do that:

awk '/VIP/ && substr($3, 1,1) == 2 {print $1,$3}' | perl -lne 'm{"(.*?)".*?//(.*):(\d+)/} and print "$2 $3"' | sort -u -k2,2

 

Though I'm not quite sure what that perl script does.

Nor why you have both perl and awk (and grep) in the same pipeline.  Aren't you afraid they might start a fight? :smileytongue:

James R. Ferguson
Acclaimed Contributor

Re: Sort by port range

Hi Allan:

 

Dennis is right --- this is awful mixing Perl and 'awk' in a pipeline.  I vaguely remember a post (or two) of yours where I suggested the regular expression for matching, but that's about all.

 

It would be nice if you posted your *raw* input and then what you would like it distilled into!

 

Regards!

 

...JRF...

allanm77
Frequent Advisor

Re: Sort by port range

Hi JRF,Dennis,

 

Here is the raw and distilled outputs -

 

Raw Output -

 "AccountService" = "http://foobar.com:2000/cgi-bin/JBOSS/app/acct";
 "StaticApp" = "http://foobar.com:4001/service/content";
 "ContentApp" = "http://foobar.com:3005/cgi-bin/JBOSS/App.jbs/content";

 




desired output 1 -

foobar.com  2000
foobar.com  4001
foobar.com  3005

desired output 2 -

AccountService foobar.com 2000
StaticApp foobar.com 4001
ContentApp foobar.com 3005

 Thanks,

Allan.

James R. Ferguson
Acclaimed Contributor

Re: Sort by port range

Hi (again):

 

Well, long time, no hear :-)

 

Anyway:

 

 

# perl -lne 'm{"(.*?)".*?//(.*):(\d+)/} and print "$2 $3"' FILE |sort -u -k2,2

or (of course, in a pipeline):

# ... | perl -lne 'm{"(.*?)".*?//(.*):(\d+)/} and print "$2 $3"' | sort -u -k2,2

 

gives:

 

foobar.com 2000
foobar.com 4001
foobar.com 3005

 

AND:

 

 

# perl -lne 'm{"(.*?)".*?//(.*):(\d+)/} and print "$1 $2 $3"' FILE

(or):

# ... | perl -lne 'm{"(.*?)".*?//(.*):(\d+)/} and print "$1 $2 $3"'

yields:

 

AccountService foobar.com 2000
StaticApp foobar.com 4001
ContentApp foobar.com 3005

Regards!

 

...JRF...

Dennis Handly
Acclaimed Contributor

Re: Sort by port range

Do you still want to limit the output to one of the 2xxx, 3xxx or 4xxx series?

James R. Ferguson
Acclaimed Contributor

Re: Sort by port range

Hi (again):

 

Oops, there's at least one correction that should be made.  Since the port numbers are numeric we need to tell the sort that:

 

# ... | perl -lne 'm{"(.*?)".*?//(.*):(\d+)/} and print "$2 $3"' | sort -nk2,2

 

# ... | perl -lne 'm{"(.*?)".*?//(.*):(\d+)/} and print "$1 $2 $3"' | sort -nk3,3

Notice that I dropped the '-u' sort option.  It's not clear why (we) used that.  If want to restrict the output to some range of port numbers as this post suggestes, do:
# ... | perl -lne 'm{"(.*?)".*?//(.*):(\d+)/} && $3>=2000 && $3<=2999 and print "$2 $3"'

or:

# ...| perl -lne 'm{"(.*?)".*?//(.*):(\d+)/} && $3>=2000 && $3<=2999 and print "$1 $2 $3"' 

...depending on the output format for which you asked.  You can pipe those results to 'sort -nk2,2' or 'sort -nk3,3' respectively to order the output by ascending port number.

 

...JRF...