Sort
Jump to navigation
Jump to search
Random sorting
While GNU/sort has the -R
option to sort randomly, the GNU/sort version on MacOS X is much older and lacks this option.[1] We can use Perl[2] as a substitute though:
$ cat foo 1 2 3 $ perl -MList::Util=shuffle -e 'print shuffle(<STDIN>);' < foo 3 1 2
With GNU/sort 8.13:[1]
$ sort -R foo 1 3 2
Remove duplicates without sorting
Consider this file:
$ cat foo AAA CCC AAA BBB
With sort (or sort & uniq), we can remove the duplicates, but the original order is destroyed:
$ sort -u foo AAA BBB CCC
AWK to the rescue![3]
$ awk '!x[$0]++' foo AAA CCC BBB
Sorting by line length
$ cat foo some line a longer line an even longer line line a short line
Sort by line length with awk
:
$ awk '{print length, $0 }' foo | sort -n -s | cut -d" " -f2- line some line a short line a longer line an even longer line
Sorting in Vim
To sort only a range within the file, from the cursor to the next matching ],
at the end of a line:
criticalpatterns => [ ░'kernel:', 'ipsec:', 'portmap:', ], :.,/\],$/ sort u
The u
removes duplicate lines too.