in reply to simple question about printing vars

Use reference as the filename:
$query = `netstat -ntu | awk '{print \$4}' | sort -nr`; open my $FH, '<', \$query; while (<$FH>) { ... }
Also, consider doing the awk's work and sorting in Perl, too.

Replies are listed 'Best First'.
Re^2: simple question about printing vars
by httpd (Novice) on Nov 03, 2011 at 13:07 UTC

    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?
      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 :)
      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 ;)