Merge pull request #2663 from cvsher/master

[translated]20150417 14 Useful Examples of Linux 'sort' Command--Part 1
This commit is contained in:
Xingyu.Wang 2015-04-24 14:53:03 +08:00
commit 8643783653
2 changed files with 132 additions and 132 deletions

View File

@ -1,132 +0,0 @@
translating by cvsher
14 Useful Examples of Linux sort Command Part 1
================================================================================
Sort is a Linux program used for printing lines of input text files and concatenation of all files in sorted order. Sort command takes blank space as field separator and entire Input file as sort key. It is important to notice that sort command dont actually sort the files but only print the sorted output, until your redirect the output.
This article aims at deep insight of Linux sort command with 14 useful practical examples that will show you how to use sort command in Linux.
### 1. First we will be creating a text file (tecmint.txt) to execute sort command examples. Our working directory is /home/$USER/Desktop/tecmint. ###
The option -e in the below command enables interpretion of backslash and /n tells echo to write each string to a new line.
$ echo -e "computer\nmouse\nLAPTOP\ndata\nRedHat\nlaptop\ndebian\nlaptop" > tecmint.txt
![Split String by Lines in Linux](http://www.tecmint.com/wp-content/uploads/2015/04/Split-String-by-Lines.gif)
### 2. Before we start with sort lets have a look at the contents of the file and the way it look. ###
$ cat tecmint.txt
![Check Content of File](http://www.tecmint.com/wp-content/uploads/2015/04/Check-Content-of-File.gif)
### 3. Now sort the content of the file using following command. ###
$ sort tecmint.txt
![Sort Content of File linux](http://www.tecmint.com/wp-content/uploads/2015/04/Sort-Content.gif)
**Note**: The above command dont actually sort the contents of text file but only show the sorted output on terminal.
### 4. Sort the contents of the file tecmint.txt and write it to a file called (sorted.txt) and verify the content by using [cat command][1]. ###
$ sort tecmint.txt > sorted.txt
$ cat sorted.txt
![Sort File Content in Linux](http://www.tecmint.com/wp-content/uploads/2015/04/Sort-File-Content.gif)
### 5. Now sort the contents of text file tecmint.txt in reverse order by using -r switch and redirect output to a file reversesorted.txt. Also check the content listing of the newly created file. ###
$ sort -r tecmint.txt > reversesorted.txt
$ cat reversesorted.txt
![Sort Content By Reverse](http://www.tecmint.com/wp-content/uploads/2015/04/Sort-Content-By-Reverse.gif)
### 6. We are going a create a new file (lsl.txt) at the same location for detailed examples and populate it using the output of ls -l for your home directory. ###
$ ls -l /home/$USER > /home/$USER/Desktop/tecmint/lsl.txt
$ cat lsl.txt
![Populate Output of Home Directory](http://www.tecmint.com/wp-content/uploads/2015/04/Populate-Output.gif)
Now will see examples to sort the contents on the basis of other field and not the default initial characters.
### 7. Sort the contents of file lsl.txt on the basis of 2nd column (which represents number of symbolic links). ###
$ sort -nk2 lsl.txt
**Note**: The -n option in the above example sort the contents numerically. Option -n must be used when we wanted to sort a file on the basis of a column which contains numerical values.
![Sort Content by Column](http://www.tecmint.com/wp-content/uploads/2015/04/Sort-Content-by-Column.gif)
### 8. Sort the contents of file lsl.txt on the basis of 9th column (which is the name of the files and folders and is non-numeric). ###
$ sort -k9 lsl.txt
![Sort Content Based on Column](http://www.tecmint.com/wp-content/uploads/2015/04/Sort-Content-Based-on-Column.gif)
### 9. It is not always essential to run sort command on a file. We can pipeline it directly on the terminal with actual command. ###
$ ls -l /home/$USER | sort -nk5
![Sort Content Using Pipe Option](http://www.tecmint.com/wp-content/uploads/2015/04/Sort-Content-By-Pipeline.gif)
### 10. Sort and remove duplicates from the text file tecmint.txt. Check if the duplicate has been removed or not. ###
$ cat tecmint.txt
$ sort -u tecmint.txt
![Sort and Remove Duplicates](http://www.tecmint.com/wp-content/uploads/2015/04/Sort-and-Remove-Duplicates.gif)
Rules so far (what we have observed):
- Lines starting with numbers are preferred in the list and lies at the top until otherwise specified (-r).
- Lines starting with lowercase letters are preferred in the list and lies at the top until otherwise specified (-r).
- Contents are listed on the basis of occurrence of alphabets in dictionary until otherwise specified (-r).
- Sort command by default treat each line as string and then sort it depending upon dictionary occurrence of alphabets (Numeric preferred; see rule 1) until otherwise specified.
### 11. Create a third file lsla.txt at the current location and populate it with the output of ls -lA command. ###
$ ls -lA /home/$USER > /home/$USER/Desktop/tecmint/lsla.txt
$ cat lsla.txt
![Populate Output With Hidden Files](http://www.tecmint.com/wp-content/uploads/2015/04/Populate-Output-With-Hidden-Files.gif)
Those having understanding of ls command knows that ls -lA=ls -l + Hidden files. So most of the contents on these two files would be same.
### 12. Sort the contents of two files on standard output in one go. ###
$ sort lsl.txt lsla.txt
![Sort Contents of Two Files](http://www.tecmint.com/wp-content/uploads/2015/04/Sort-Content-of-Multiple-Files.gif)
Notice the repetition of files and folders.
### 13. Now we can see how to sort, merge and remove duplicates from these two files. ###
$ sort -u lsl.txt lsla.txt
![Sort, Merge and Remove Duplicates from File](http://www.tecmint.com/wp-content/uploads/2015/04/Sort-Merge-Duplicates-Content.gif)
Notice that duplicates has been omitted from the output. Also, you can write the output to a new file by redirecting the output to a file.
### 14. We may also sort the contents of a file or the output based upon more than one column. Sort the output of ls -l command on the basis of field 2,5 (Numeric) and 9 (Non-Numeric). ###
$ ls -l /home/$USER | sort -t "," -nk2,5 -k9
![Sort Content By Field Column](http://www.tecmint.com/wp-content/uploads/2015/04/Sort-Content-By-Field-Column.gif)
Thats all for now. In the next article we will cover a few more examples of sort command in detail for you. Till then stay tuned and connected to Tecmint. Keep sharing. Keep commenting. Like and share us and help us get spread.
--------------------------------------------------------------------------------
via: http://www.tecmint.com/sort-command-linux/
作者:[Avishek Kumar][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[a]:http://www.tecmint.com/author/avishek/
[1]:http://www.tecmint.com/13-basic-cat-command-examples-in-linux/

View File

@ -0,0 +1,132 @@
Linux sort命令的14个有用的范例 -- 第一部分
=============================================================
Sort是用于对单个或多个文本文件内容进行排序的Linux程序。Sort命令以空格作为字段分隔符将一行分割为多个关键字对文件进行排序。需要注意的是除非你将输出重定向到文件中否则Sort命令并不对文件内容进行实际的排序(即文件内容没有修改),只是将文件内容按有序输出。
本文的目标是通过14个实际的范例让你更深刻的理解如何在Linux中使用sort命令。
###1. 首先我们将会创建一个用于执行sort命令的文本文件tecmint.txt。工作路径是/home/$USER/Desktop/tecmint。###
下面命令中的‘-e选项将/’和‘/n解析成一个新行
$ echo -e "computer\nmouse\nLAPTOP\ndata\nRedHat\nlaptop\ndebian\nlaptop" > tecmint.txt
![Split String by Lines in Linux](http://www.tecmint.com/wp-content/uploads/2015/04/Split-String-by-Lines.gif)
###2. 在开始学习sort命令前我们先看看文件的内容及其显示方式。###
$ cat tecmint.txt
![Check Content of File](http://www.tecmint.com/wp-content/uploads/2015/04/Check-Content-of-File.gif)
###3. 现在,使用如下命令对文件内容进行排序。###
$ sort tecmint.txt
![Sort Content of File linux](http://www.tecmint.com/wp-content/uploads/2015/04/Sort-Content.gif)
**注意**:上面的命令并不对文件内容进行实际的排序,仅仅是将其内容按有序方式输出。
###4. 对文件tecmint.txt文件内容排序并将排序后的内容输出到名为sorted.txt的文件中然后使用[cat][1]命令查看验证sorted.txt文件的内容。###
$ sort tecmint.txt > sorted.txt
$ cat sorted.txt
![Sort File Content in Linux](http://www.tecmint.com/wp-content/uploads/2015/04/Sort-File-Content.gif)
###5. 现在使用‘-r参数对tecmint.txt文件内容进行逆序排序并将输出内容重定向到reversesorted.txt文件中并使用cat命令查看文件的内容。###
$ sort -r tecmint.txt > reversesorted.txt
$ cat reversesorted.txt
![Sort Content By Reverse](http://www.tecmint.com/wp-content/uploads/2015/04/Sort-Content-By-Reverse.gif)
###6. 创建一个新文件lsl.txt文件内容为在home目录下执行ls -l命令的输出。###
$ ls -l /home/$USER > /home/$USER/Desktop/tecmint/lsl.txt
$ cat lsl.txt
![Populate Output of Home Directory](http://www.tecmint.com/wp-content/uploads/2015/04/Populate-Output.gif)
我们将会看到对其他基础字段进行排序的例子,而不是对默认的初始字符进行排序。
###7. 基于第二列符号连接的数量对文件lsl.txt进行排序。###
$ sort -nk2 lsl.txt
**注意**:上面例子中的‘-n参数表示对数值内容进行排序。当想基于文件中的数值列对文件进行排序时必须要使用-n参数。
![Sort Content by Column](http://www.tecmint.com/wp-content/uploads/2015/04/Sort-Content-by-Column.gif)
###8. 基于第9列文件和目录的名称非数值对文件lsl.txt进行排序。###
$ sort -k9 lsl.txt
![Sort Content Based on Column](http://www.tecmint.com/wp-content/uploads/2015/04/Sort-Content-Based-on-Column.gif)
###9. sort命令并非仅能对文件进行排序我们还可以通过管道将命令的输出内容重定向到sort命令中。###
$ ls -l /home/$USER | sort -nk5
![Sort Content Using Pipe Option](http://www.tecmint.com/wp-content/uploads/2015/04/Sort-Content-By-Pipeline.gif)
###10. 对文件tecmint.txt进行排序并删除重复的行。然后检查重复的行是否已经删除了。###
$ cat tecmint.txt
$ sort -u tecmint.txt
![Sort and Remove Duplicates](http://www.tecmint.com/wp-content/uploads/2015/04/Sort-and-Remove-Duplicates.gif)
目前我们发现的排序规则:
除非指定了‘-r参数否则排序的优先级按下面规则排序
- 以数字开头的行优先级最高
- 以小写字母开头的行优先级次之
- 待排序内容按字典序进行排序
- 默认情况下sort命令将带排序内容的每行关键字当作一个字符串进行字典序排序数字优先级最高参看规则 - 1
###11. 创建文件lsla.txt其内容用ls -la命令的输出内容填充。###
$ ls -lA /home/$USER > /home/$USER/Desktop/tecmint/lsla.txt
$ cat lsla.txt
![Populate Output With Hidden Files](http://www.tecmint.com/wp-content/uploads/2015/04/Populate-Output-With-Hidden-Files.gif)
了解ls命令的读者都知道ls -la=ls -l + 隐藏文件。因此这两个文件的大部分内容都是相同的。
###12. 对上面两个文件内容进行排序输出。###
$ sort lsl.txt lsla.txt
![Sort Contents of Two Files](http://www.tecmint.com/wp-content/uploads/2015/04/Sort-Content-of-Multiple-Files.gif)
注意文件和目录的重复
###13. 现在我们看看怎样对两个文件进行排序、合并,并且删除重复行。###
$ sort -u lsl.txt lsla.txt
![Sort, Merge and Remove Duplicates from File](http://www.tecmint.com/wp-content/uploads/2015/04/Sort-Merge-Duplicates-Content.gif)
此时,我们注意到重复的行已经被删除了,我们可以将输出内容重定向到文件中。
###14. 我们同样可以基于多列对文件内容进行排序。基于第2,5数值和9非数值列对ls -l命令的输出进行排序。###
$ ls -l /home/$USER | sort -t "," -nk2,5 -k9
![Sort Content By Field Column](http://www.tecmint.com/wp-content/uploads/2015/04/Sort-Content-By-Field-Column.gif)
先到此为止了在接下来的文章中我们将会学习到sort命令更多的详细例子。届时敬请关注Tecmint。保持分享精神。若喜欢本文敬请将本文分享给你的朋友。
--------------------------------------------------------------------------------
via: http://www.tecmint.com/sort-command-linux/
作者:[Avishek Kumar][a]
译者:[cvsher](https://github.com/cvsher)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[a]:http://www.tecmint.com/author/avishek/
[1]:http://www.tecmint.com/13-basic-cat-command-examples-in-linux/