User Tools

Site Tools


unix_commands:sort

The sort command in Linux and Unix-like systems is used to sort the lines of a text file or the output of a command. It can sort the lines alphabetically or numerically and can also sort in reverse order.

Here is the basic syntax for the sort command:

sort [options] [file]

file is the name of the file you want to sort.

Here are some examples of the sort command:

1. To sort the lines of a file called “file1.txt” in alphabetical order:

$ sort file1.txt

2. To sort the lines of a file called “file1.txt” in reverse order

$ sort -r file1.txt

Note: The -r option is used to sort the lines in reverse order.

3. To sort the output of a command

$ ls -la | sort

Note: The | symbol is used to pipe the output of one command to another. In this case it is piping the output of the ls -la command to the sort command.

4. To sort the lines of a file numerically

$ sort -n file1.txt

Note: The -n option is used to sort the lines numerically.

5. To ignore leading blanks while sorting

$ sort -b file1.txt

Note: The -b option is used to ignore leading blanks while sorting

6. To sort the lines based on the second field

$ sort -k 2 file1.txt

Note: The -k option is used to sort the lines based on a specific field

unix_commands/sort.txt · Last modified: 2023/01/16 08:11 by admin

CIFNet is a leading hosting solution provider, offering a wide range of services including IP bandwidth, co-location, VMware and KVM hosting.
Host Your Site with CIFNet!