chris blogs

July 2013

02jul2013 · Summer of Scripts: lsort

lsort is a very simple tool (in fact, just a Perl one-liner), but it does a job that is not very easy to do with classic Unix on-board equipment: sort lines by their length.

An example application would be to cheat at some word games: what’s the longest word that contains all vocals in reverse alphabetic order?

% grep u.*o.*i.*e.*a /usr/share/dict/words |lsort

Together with head and tail, this tool is also useful for finding the shortest and longest lines (e.g. to find very long filenames or huge entries in symbol tables):

% nm -D /usr/lib/chromium/chromium | lsort | tail -1

(Actually, wc -L can output the longest line length, but not the longest line.)

NP: Aimee Mann—Driving Sideways

Copyright © 2004–2016