Comments on Perl one-liner to see who is hogging all the open filehandles Imagined things 2018-03-23T15:50:44Z http://www.imaginarybillboards.com/?feed=atom&p=194 WordPress By: Chris Chris http://imaginarybillboards.com http://www.imaginarybillboards.com/?p=194#comment-630 2011-03-23T14:32:22Z 2011-03-23T14:32:22Z I’m so stealing both of those in the future – thanks both of you.

]]>
By: Simon Flack Simon Flack http://www.imaginarybillboards.com/?p=194#comment-629 2011-03-23T14:15:56Z 2011-03-23T14:15:56Z 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 oylenshpeegul http://oylenshpeegul.typepad.com http://www.imaginarybillboards.com/?p=194#comment-626 2011-03-22T23:59:33Z 2011-03-22T23:59:33Z 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.

]]>