4 ms·
Sorting by length is an interesting programming problem; it can't be done by a regular expression. One way is to use a mask: % sed -e "s/./x/g" < original.
by jloughry 13y ago
Sorting by length is an interesting programming problem; it can't be done by a regular expression. One way is to use a mask:
% sed -e "s/./x/g" < original.txt > mask.txt
% paste mask.txt original.txt | sort | cut -f 2
% rm mask.txt
- jedberg 13y agoI did this to make the above: sort /tmp/foo |awk '{ print length, $0 }'|sort -n|awk '{print $1}'|uniq -c|awk '{r="";i=s=$1/10;while(i-->0)r=r"#";printf "%15s %5d %s %s",$2,$1,r,"\n";}'
- binarymax 13y agoGolf time! in js: require('fs').readFileSync('list.txt').split('\n').sort(function(a,b){return b.length-a.length});
- gamegoblin 13y agoPython: sorted(open("list.text").readlines(),key=len) Haskell: (sortBy (comparing length).lines.readFile) "list.txt"
- jloughry 13y agoEliminate the temporary file: sed -e "s/./x/g" < list.txt | paste - list.txt | sort | cut -f 2
- jloughry 13y agoWe can eliminate the paste by using sed's hold buffer, but it costs another regexp to get rid of the spurious newline: sed -e "h;s/./x/g;G;s/\n/\t/g" list.txt | sort | cut -f2