Bash - sort

sort

  1. sort a space-delimited file based on its first column, then the second if the first is the same, and so on:
sort input.txt
  1. sort a huge file (GNU sort ONLY):
sort -S 1500M -t $HOME/tmp input.txt > sorted.txt
  1. sort starting from the third column, skipping the first two columns:
sort +2 input.txt
  1. sort the second column as numbers, descending order; if identical, sort the 3rd as strings, ascending order:
sort -k2,2nr -k3,3 input.txt
  1. sort starting from the 4th character at column 2, as numbers:
sort -k2.4n input.txt
  1. Sort and remove duplicate lines in a file in one step without intermediary files

Explanation

We open a file with vi and run two vi commands (specified with +): %!sort | uniq % = range definition, it means all the lines in the current buffer. ! = run filter for the range specified. Filter is an external program, in this example sort | uniq wq = write contents to file and quit. vi +’%!sort | uniq’ +wq file.txt