[Israel.pm] One-liner perl question

Assaf Gordon gordon at cshl.edu
Mon Sep 10 18:55:03 PDT 2012


On 09/09/2012 01:39 PM, ynon perek wrote:
> I'm trying to turn a short program into a one-liner. The program takes an argument "n" and prints the first n file names sorted by file name length, with no duplicates in length.

I know you've asked for a *Perl* one-liner, but since a recent suggested solution already "cheat" by using the shell to get the list of files, why not just do it entirely in using standard command line programs ?

## prints the top 5 longest file names, remove the "r" from sort to print the shortest.
ls | awk '{print length($0), $0}' | sort -nru | cut -f2 -d' ' | head -n5

Seems cleaner, and shorter than any Perl solution...


More information about the Perl mailing list