lsof | perl -anE '$c{$F[2]}++ }END{say "$_ $c{$_}" for (keys %c);'
=) Which is essentially piping lsof into:
use feature 'say';
while (defined($_ = )) {
our(@F) = split(' ', $_, 0);
++$c{$F[2]};
}
sub END {
say "$_ $c{$_}" foreach (keys %c);
}
see `perldoc perlrun` for more info
]]>You can use -l to get Perl to put that newline on for you.
perl -le ‘map{$c{(split(/\s+/))[2]}++} `lsof`;print “$_ $c{$_}” for keys %c;’
If we move the lsof call outside, then we can use -n to do the map
lsof | perl -nle ‘$c{(split(/\s+/))[2]}++; END{print “$_ $c{$_}” for keys %c}’
Then we can use -a to do the split
lsof | perl -anle ‘$c{$F[2]}++; END{print “$_ $c{$_}” for keys %c}’
With a recent perl, we can use say instead of print with -l
lsof | perl -anE ‘$c{$F[2]}++; END{say “$_ $c{$_}” for keys %c}’
The -l does a chomp on the way in too, but we didn’t really make use of that here, so say is all we need.
]]>