Bash - sort
sort
- 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
- sort a huge file (GNU sort ONLY):
sort -S 1500M -t $HOME/tmp input.txt > sorted.txt
- sort starting from the third column, skipping the first two columns:
sort +2 input.txt
- sort the second column as numbers, descending order; if identical, sort the 3rd as strings, ascending order:
sort -k2,2nr -k3,3 input.txt
- sort starting from the 4th character at column 2, as numbers:
sort -k2.4n input.txt
- 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
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.