Comments on: Perl one-liner to see who is hogging all the open filehandles http://www.imaginarybillboards.com/?p=194 Imagined things Fri, 23 Mar 2018 15:50:44 +0000 hourly 1 https://wordpress.org/?v=6.0.9 By: Chris http://www.imaginarybillboards.com/?p=194&cpage=1#comment-630 Wed, 23 Mar 2011 14:32:22 +0000 http://www.imaginarybillboards.com/?p=194#comment-630 I’m so stealing both of those in the future – thanks both of you.

]]>
By: Simon Flack http://www.imaginarybillboards.com/?p=194&cpage=1#comment-629 Wed, 23 Mar 2011 14:15:56 +0000 http://www.imaginarybillboards.com/?p=194#comment-629 perl has some nice switches to simplify this a bit:


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

]]>
By: oylenshpeegul http://www.imaginarybillboards.com/?p=194&cpage=1#comment-626 Tue, 22 Mar 2011 23:59:33 +0000 http://www.imaginarybillboards.com/?p=194#comment-626 Ooh, I love one-liners! I’m not really much of a golfer, but here’s a few things you might do with flags:

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.

]]>