Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: simple question about printing vars

by httpd (Novice)
on Nov 03, 2011 at 13:07 UTC ( #935655=note: print w/replies, xml ) Need Help??


in reply to Re: simple question about printing vars
in thread simple question about printing vars

thanks, so as far as I understand using FILE descriptor is the best solution, right?

as for awk, uniq and sort bash commands, what is the equivalent in perl for that functions, or should I use regexp?

for example: I need to sort IPs from netstat command, look:
# netstat -ntu | egrep 'udp|tcp' | awk '{print $4}' | sort | uniq -c | + sort -nr 17 24.13.201.116:80 1 44.43.121.146:22

how can I do the same using just perl functions?

Is it better to use perl native functions for such purpose instead of calling external shell commands?

Replies are listed 'Best First'.
Re^3: simple question about printing vars
by choroba (Bishop) on Nov 03, 2011 at 13:40 UTC
    Something like this?
    netstat -ntu | perl -nae 'next unless $F[0] =~ /tcp|udp/; $seen{$F[4]}++ } { @keys = sort { $seen{$b} <=> $seen{$a} } keys %seen; printf "% " . (1 + length($seen{$keys[0]})) . "d %s\n", $seen{$_}, $_ for @keys '

      thanks. but more complicated :)

      but is it justified to use perl native sort instead of external commands? is it less resource consumable? or faster?

      or is it simpler to compile script without external commands?

      if we go further, then we can even use perl native function for invoke netstat information directly from /proc :)
Re^3: simple question about printing vars
by reisinge (Friar) on Nov 03, 2011 at 14:48 UTC
    Maybe like this?
    #!/usr/bin/perl use strict; use warnings; open PO, "netstat -na|" or die "Can't pipe open: $!\n"; my %count; while (<PO>) { next unless /^udp/ or /^tcp/; my @fields = split; # split $_ on whitespace $count{$fields[3]}++; ## next jumps here } close PO; for my $ip (sort keys %count) { print "\t$count{$ip} $ip\n"; }

    Have a nice day, j

      I will try that, thanks ;)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://935655]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2018-11-14 09:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My code is most likely broken because:
















    Results (166 votes). Check out past polls.

    Notices?