深入了解Linux命令中的排序方法(linux命令排序)

Sort command is the most commonly used command for sorting files in Linux. It is a command line utility that can be used to sort files according to various criteria, like alphabetically, numerically and chronologically.

Sort command provides many options/flags to sort files in different manners. It also accepts options to sort in reverse order, to ignore differences between folded and upper/lower cases and to discard empty lines.

Example 1:

Sort Alphabetically

The below example shows how to sort a file containing a list of words in alphabetic order.

$sort words.txt

apple

banana

cat

dog

Example 2:

Sort Numerically

The below example shows how to sort a file containing a list of numbers in a numerical order.

$sort -n numbers.txt

1

2

3

4

Example 3:

Sort in Reverse order

The below example shows how to sort a file containing a list of words in reverse alphabetical order.

$sort -r words.txt

dog

cat

banana

apple

Example 4:

Ignore Case differences

The below example shows how to sort a file containing a list of words ignoring their case differences.

$sort -f words.txt

apple

banana

Cat

dog

Example 5:

Discard empty lines

The below example shows how to sort a file containing a list of words and dropping empty lines in the output.

$sort -z words.txt

apple

banana

cat

dog

In conclusion, the sort command is a useful utility to arrange files in Linux according to various criteria. It provides many options which can be used to sort files with different conditions and produce expected result. With the help of these examples, you can easily understand more about sorting files using this command.


数据运维技术 » 深入了解Linux命令中的排序方法(linux命令排序)