Sort

From Segfault
Jump to: navigation, 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 in Vi

To sort only a range within the file, from the cursor to the next matching ],:

criticalpatterns => [
         ░'kernel:',
          'ipsec:',
          'portmap:',
],



:.,/\],/ sort

References