The sort Command
The sort
command is used to sort text files on Unix-like operating systems. It is a very flexible tool that can be used to sort
lines of text in a variety of different ways, including:
- Alphabetical order
- Numerical order
- Chronological order
- Custom order
Syntax
The basic syntax for the sort
command is as follows:
sort [options] <file>
The <file> argument is the path to the file that you want to sort
. If you do not specify a file, the sort
command will read from STDIN.
The [options] argument is a list of options that can be used to modify the behavior of the sort
command.
Examples
Sort the file “myfile.txt” in ascending alphabetical order.
sort myfile.txt
Sort the file “myfile.txt” in descending numerical order.
sort -n -r myfile.txt
Sort the file “myfile.txt” by the second column.
sort -k 2 myfile.txt
Sort the file “myfile.txt” by the date and time in the first column.
sort -M -k 1 myfile.txt
Options
The sort
command has a number of options that can be used to modify its behavior. Some of the most common options are:
-n: Sort the lines of text numerically.
-r: Sort the lines of text in reverse order.
-k <key>: Sort the lines of text by the specified key.
-u: Remove duplicate lines from the output.
For more information on the sort
command and its options, please see the man page: man sort
.
With gencmd
gencmd -c sort sort a file in reverse order based on 3rd column sort -r -k3 file.txt sort -r -k 3 < file sort -k3r file.txt sort -r -k 3 file