linux基本命令示例
Linux sort
command is used for sorting file content in a particular order. It supports sorting of files alphabetically (ascending or descending), numerically, in reverse order, etc. We can also remove duplicate lines from the file. In this article, we’ll see different example usages of the Linux sort command.
Linux sort
命令用于按特定顺序对文件内容进行排序。 它支持按字母顺序(升序或降序),数字,反向顺序等对文件进行排序。我们还可以从文件中删除重复的行。 在本文中,我们将看到Linux sort命令的不同示例用法。
When using the sort command
使用sort命令时
- Lines beginning with numbers or digits appear before lines with alphabets. 以数字或数字开头的行出现在带有字母的行之前。
- Lines starting with lowercase letters will appear before lines beginning with the same letter but in uppercase. 以小写字母开头的行将出现在以相同字母开头但大写的行之前。
没有任何争论的Linux Sort命令 (Linux Sort command without any arguements)
When sort
is used without any arguments, it arranges the contents of a text file in alphabetical order. In this example, we are using the text file fruits.txt
as shown below.
当使用不带任何参数的sort
,它将按字母顺序排列文本文件的内容。 在此示例中,我们使用文本文件fruits.txt
,如下所示。
To sort in alphabetical order run:
要按字母顺序排序,请运行:
# sort fruits.txt
Output
输出量
用-r选项排序 (Sort with -r option)
If you wish to sort a file in reverse order using sort command, use the -r
option. For instance, to reverse the order of the input file in our previous example, execute:
如果希望使用sort命令以相反的顺序对文件进行排序,请使用-r
选项。 例如,要在前面的示例中反转输入文件的顺序,请执行:
# sort -r fruits.txt
Output
输出量
按数字排序 (Sort numerically)
To sort a file in a numerical order, use the -n
attribute. In this example, we have a file digits.txt
as shown below
要按数字顺序对文件排序,请使用-n
属性。 在此示例中,我们有一个文件digits.txt
,如下所示
To sort this file numerically from the smallest value to the largest value execute the command
要将文件从最小值到最大值进行数字排序,请执行以下命令
# sort -n digits.txt
Output
输出量
You can also use the -nr
option to reverse the order of the numerical results, i.e. sort from the largest to the smallest value
您也可以使用-nr
选项反转数值结果的顺序,即从最大到最小排序
To demonstrate this, we will run the command:
为了说明这一点,我们将运行以下命令:
# sort -nr digits.txt
Output
输出量
用-o选项排序 (Sort with -o option)
The -o
option allows the user to write the output of the file onto a new file. Using this option is just the same as redirecting the output to another file as shown:
-o
选项允许用户将文件的输出写入新文件。 使用此选项与将输出重定向到另一个文件相同,如下所示:
# sort fruits.txt > newfile.txt
To write the output of a file to a new file using -o
option use the syntax
要使用-o
选项将文件的输出写入新文件,请使用以下语法
# sort -o newfile.txt filename.txt
For example,
例如,
# sort sorted_fruits.txt fruits.txt
Output
输出量
用-u选项排序 (Sort with -u option)
The -u
option is used to remove file duplicates in a text file while at the same time sorting text alphabetically.
-u
选项用于删除文本文件中的文件重复项,同时按字母顺序对文本进行排序。
In this example, we are going to use cars.txt
as shown below
在这个例子中,我们将使用cars.txt
,如下所示
As you can see, we have 2 duplicate car models: Toyota and Porsche.
如您所见,我们有两种重复的汽车模型: Toyota和Porsche 。
To remove these duplicates and sort alphabetically, run
要删除这些重复项并按字母顺序排序,请运行
# sort -u cars.txt
Output
输出量
用-k选项排序 (Sort with -k option)
The -k
option sorts a table according to the column number. For instance, if you have a table with 2 columns, use -k -2n
option to sort the second column.
-k
选项根据列号对表进行排序。 例如,如果您有一个包含2列的表,请使用-k -2n
选项对第二列进行排序。
In this example, we have the text file bonus.txt
在此示例中,我们有文本文件bonus.txt
To sort the second column execute
要对第二列进行排序,请执行
# sort -k -2n bonus.txt
Output
输出量
使用-M选项排序月份 (Sort months with -M option)
Use the -M
option to sort a file containing months in ascending order i.e { Jan , Feb, … Nov, Dec }
使用-M
选项以升序对包含月份的文件进行排序,即{Jan,Feb,…Nov,Dec}
We have a file months.txt as shown below
我们有一个文件months.txt,如下所示
To sort the months in order execute:
要对月份排序以便执行:
# sort -M months.txt
检查sort命令的版本 (Check the version of sort command)
To check the version od sort command run
检查版本od sort命令运行
$ sort --version
Output
输出量
排序命令中的更多选项 (More options in sort command)
To acquaint yourself with more commands other than the ones we’ve covered, visit the sort man page
要使自己熟悉除我们介绍的命令以外的其他命令,请访问sort手册页
# man sort
翻译自: https://www.journaldev.com/29025/sort-command-linux-examples
linux基本命令示例