编程小贴士

给你的编程提供小点子


Linux 的 ‘sort’命令的14个有用的范例

Sort是用于对单个或多个文本文件内容进行排序的Linux程序。Sort命令以空格作为字段分隔符,将 一行分割为多个关键字对文件进行排序。需要注意的是除非你将输出重定向到文件中,否则Sort命令并不对文件内容进行实际的排序(即文件内容没有修改), 只是将文件内容按有序输出。

本文的目标是通过14个实际的范例让你更深刻的理解如何在Linux中使用sort命令。

tips1000_2015-06-14_06-01-5

1、 首先我们将会创建一个用于执行‘sort’命令的文本文件(tecmint.txt)。工作路径是‘/home/$USER/Desktop/tecmint’。

下面命令中的‘-e’选项将启用‘\’转义,将‘\n’解析成换行

$ echo -e "computer\nmouse\nLAPTOP\ndata\nRedHat\nlaptop\ndebian\nlaptop" > tecmint.txt

tips2000_2025-06-24_06-02-552、 在开始学习‘sort’命令前,我们先看看文件的内容及其显示方式。

$ cat tecmint.txt

tips3000_3035-06-34_06-03-553、 现在,使用如下命令对文件内容进行排序。

$ sort tecmint.txt

tips1000_2015-06-14_06-01-55注意:上面的命令并不对文件内容进行实际的排序,仅仅是将其内容按有序方式输出。

4、 对文件‘tecmint.txt’文件内容排序,并将排序后的内容输出到名为sorted.txt的文件中,然后使用cat命令查看验证sorted.txt文件的内容。

$ sort tecmint.txt > sorted.txt
$ cat sorted.txt

tips1000_2015-06-14_06-01-55、 现在使用‘-r’参数对‘tecmint.txt’文件内容进行逆序排序,并将输出内容重定向到‘reversesorted.txt’文件中,并使用cat命令查看文件的内容。

$ sort -r tecmint.txt > reversesorted.txt
$ cat reversesorted.txt

tips5000_5055-06-55_06-05-556、 创建一个新文件(lsl.txt),文件内容为在home目录下执行‘ls -l’命令的输出。

$ ls -l /home/$USER > /home/$USER/Desktop/tecmint/lsl.txt
$ cat lsl.txt

tips4000_4045-06-44_06-04-55我们将会看到对其他字段进行排序的例子,而不是对默认的开始字符进行排序。

7、 基于第二列(符号连接的数量)对文件‘lsl.txt’进行排序。

$ sort -nk2 lsl.txt

注意:上面例子中的‘-n’参数表示对数值内容进行排序。当想基于文件中的数值列对文件进行排序时,必须要使用‘-n’参数。

tips1000_2015-06-14_06-01-58、 基于第9列(文件和目录的名称,非数值)对文件‘lsl.txt’进行排序。

$ sort -k9 lsl.txt

tips1000_2015-06-14_06-01-59、 sort命令并非仅能对文件进行排序,我们还可以通过管道将命令的输出内容重定向到sort命令中。

$ ls -l /home/$USER | sort -nk5

tips2000_2025-06-24_06-02-5710、 对文件tecmint.txt进行排序,并删除重复的行。然后检查重复的行是否已经删除了。

$ cat tecmint.txt
$ sort -u tecmint.txt

tips3000_3035-06-34_06-03-57目前我们发现的排序规则:

除非指定了‘-r’参数,否则排序的优先级按下面规则排序

  • 以数字开头的行优先级最高
  • 以小写字母开头的行优先级次之
  • 待排序内容按字典序进行排序
  • 默认情况下,‘sort’命令将带排序内容的每行关键字当作一个字符串进行字典序排序(数字优先级最高,参看规则 1)

11、 在当前位置创建第三个文件‘lsla.txt’,其内容用‘ls -lA’命令的输出内容填充。

$ ls -lA /home/$USER > /home/$USER/Desktop/tecmint/lsla.txt
$ cat lsla.txt

tips1000_2015-06-14_06-01-57了解ls命令的读者都知道‘ls -lA’ 等于 ‘ls -l’ + 隐藏文件,所以这两个文件的大部分内容都是相同的。

12、 对上面两个文件内容进行排序输出。

$ sort lsl.txt lsla.txt

tips1000_2015-06-14_06-01-58注意文件和目录的重复

13、 现在我们看看怎样对两个文件进行排序、合并,并且删除重复行。

$ sort -u lsl.txt lsla.txt

tips1000_2015-06-14_06-02-0此时,我们注意到重复的行已经被删除了,我们可以将输出内容重定向到文件中。

14、 我们同样可以基于多列对文件内容进行排序。基于第2,5(数值)和9(非数值)列对‘ls -l’命令的输出进行排序。

$ ls -l /home/$USER | sort -t "," -nk2,5 -k9

tips2000_2025-06-24_06-02-58作者:Avishek Kumar译者:cvsher校对:wxy

本文由 LCTT原创翻译,Linux中国荣誉推出

本文由 LCTT 原创翻译,Linux中国首发。也想加入译者行列,为开源做一些自己的贡献么?欢迎加入LCTT!

118 Responses to “ Linux 的 ‘sort’命令的14个有用的范例 ”

  1. chocopie说道:

    BdomWa Lovely just what I was looking for. Thanks to the author for taking his clock time on this one.

  2. i love pron说道:

    s7BJuw There as definately a lot to learn about this topic. I like all the points you made.

  3. I will start writing my own blog, definitely!

  4. Business Ladies说道:

    Wonderful put up, definitely regret not heading on the USO style dinner. Keep up the great perform!

  5. You made some clear points there. I looked on the internet for the topic and found most guys will approve with your website.

  6. Muchos Gracias for your article.Really looking forward to read more. Much obliged.

  7. mca proof说道:

    Really informative blog article.Thanks Again. Want more.

  8. We stumbled over here coming from a different web address and thought I may as well check things out. I like what I see so now i am following you. Look forward to looking into your web page yet again.

  9. Simply wanna remark that you have a very nice website , I love the style and design it really stands out.

  10. Major thanks for the blog.Much thanks again.

  11. Really informative blog.Really looking forward to read more. Much obliged.

  12. visit website说道:

    Well I truly enjoyed reading it. This subject provided by you is very effective for accurate planning.

  13. for more info说道:

    It as exhausting to seek out knowledgeable individuals on this subject, but you sound like you understand what you are speaking about! Thanks

  14. The account helped me a acceptable deal. I had been a little bit acquainted of this your broadcast offered bright

  15. Oluwadamilare说道:

    Your style is unique in comparison to other people I ave read stuff from. Thank you for posting when you ave got the opportunity, Guess I will just book mark this web site.

  16. website说道:

    That is a good tip particularly to those new to the blogosphere. Brief but very accurate info Many thanks for sharing this one. A must read post!

  17. Thanks so much for the article.Really thank you! Awesome.

  18. this说道:

    Very good post.Much thanks again. Awesome.

  19. It as nearly impossible to find educated people for this topic, but you sound like you know what you are talking about! Thanks

  20. online说道:

    I really liked your post.Much thanks again. Really Great.

  21. Very good article! We will be linking to this particularly great post on our site. Keep up the great writing.

  22. Im thankful for the blog.Really looking forward to read more. Will read on

  23. You, my pal, ROCK! I found exactly the info I already searched everywhere and simply couldn at find it. What a perfect web-site.

  24. Very nice post. I just stumbled upon your blog and wanted to say that I ave really enjoyed surfing around your blog posts. After all I will be subscribing to your feed and I hope you write again soon!

  25. private Hire说道:

    I think this is a real great blog post. Want more.

  26. Incredibly Interesting Blog site! Thank you Just for this Website!

  27. This is one awesome article.Really thank you! Great.

  28. I really liked your blog post.Much thanks again. Will read on…

  29. status说道:

    This very blog is obviously cool as well as factual. I have chosen a lot of interesting things out of this amazing blog. I ad love to return over and over again. Thanks a lot!

  30. Really informative blog post.Thanks Again. Really Great.

  31. Thanks for the blog post.Really looking forward to read more. Much obliged.

  32. There is definately a lot to learn about this issue. I love all of the points you ave made.

  33. Share说道:

    pretty valuable stuff, overall I consider this is worthy of a bookmark, thanks

  34. ma giam gia tiki说道:

    nonetheless, you command get bought an edginess over

  35. Read More Here说道:

    standard parts you happen to be familiar with but might not know how to utilize properly, along with other unique offerings in the car that ensure it is more hard to.

  36. Nwokolo说道:

    I really liked your post.Thanks Again. Great.

  37. klm coupon code说道:

    we came across a cool web site which you could love. Take a appear when you want

  38. Looking around I like to look in various places on the online world, often I will just go to Stumble Upon and read and check stuff out

  39. This particular blog is no doubt awesome and besides diverting. I have picked many interesting stuff out of it. I ad love to come back again and again. Thanks!

  40. There is visibly a lot to identify about this. I consider you made certain good points in features also.

  41. Landfill说道:

    VeаА аБТ–y goo? post. I certaаАаб‚Т€Т“nly appаА аБТ–аА аЂа•ciate

  42. Beyonce说道:

    Really enjoyed this blog post.Thanks Again. Will read on

  43. sbobet mobile说道:

    If some one wants to be updated with hottest technologies afterward he must be

  44. go to说道:

    Very interesting points you have mentioned , appreciate it for putting up. Women have been trained to speak softly and carry a lipstick. Those days are over. by Bella Abzug.

  45. Utterly written subject material, appreciate it for selective information.

  46. Thank you ever so for you blog post.Thanks Again. Want more.

  47. pay day loans说道:

    Looking forward to reading more. Great article.Really thank you! Fantastic.

  48. Looking forward to reading more. Great article.Really thank you! Want more.

  49. Thank you for your article.Really thank you! Will read on

  50. sua hat huu co说道:

    Spot on with this write-up, I seriously think this web site needs a lot more attention. I all probably be returning to see more, thanks for the information!

  51. There may be noticeably a bundle to find out about this. I assume you made certain good factors in options also.

  52. Thanks for sharing, this is a fantastic blog article.Really thank you! Great.

  53. Miracle Ndu说道:

    Wonderful goods from you, man. I ave have in mind your stuff prior to and you are just too

  54. visit website说道:

    Major thankies for the blog post.Really thank you! Will read on

  55. Emmanuel Ekanem说道:

    Regards for this tremendous post, I am glad I detected this internet site on yahoo.

  56. fitness说道:

    Valuable Website I have been checking out some of your posts and i can claim clever stuff. I will definitely bookmark your blog.

  57. Mate! This site is amazing. How did you make it look this good !

  58. referring to this article. I desire to read more things approximately it!

  59. outstanding write-up A a greater level really wonderful along with utilitarian information employing this site, likewise My own partner and we think your style is composed with fantastic works.

  60. wow, awesome post.Really thank you! Awesome.

  61. try this说道:

    Say, you got a nice article post. Really Great.

  62. red shoes说道:

    nordstrom coupon code free shipping ??????30????????????????5??????????????? | ????????

  63. worth说道:

    I value the blog.Much thanks again. Much obliged.

  64. visit website说道:

    iаЂа›?Ferienwohnungs-Urlaub direkt online suchen und buchen bei ihre-fewo.de

  65. There is definately a lot to find out about this issue. I really like all of the points you made.

  66. You made some good points there. I did a search on the topic and found most people will go along with with your blog.

  67. There as definately a great deal to know about this subject. I like all of the points you have made.

  68. Waec expo说道:

    Major thanks for the article.Really thank you! Great.

  69. School news说道:

    Say, you got a nice blog article.Thanks Again. Awesome.

  70. This blog was how do you say it? Relevant!! Finally I ave found something that helped me. Appreciate it!

  71. This blog was how do I say it? Relevant!! Finally I ave found something which helped me. Kudos!

  72. The whole look of your web site is fantastic, let well as the content material! Excellent weblog right here! Additionally your site quite a bit up very fast!

  73. some of the information you provide here. Please let me know if this okay with you.

  74. you made running a blog glance easy. The total glance of

  75. Online Article Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose

  76. You ave done a formidable task and our whole group shall be grateful to you.

  77. There as certainly a great deal to find out about this subject. I really like all of the points you made.

  78. This article actually helped me with a report I was doing.

  79. Major thankies for the post.Really thank you! Will read on

  80. That is a beautiful shot with very good light-weight -)

  81. Prior to the game for the Falcons to hold on

  82. darey说道:

    will need toHaHa). You certainly put a brand new spin on a subject which has been written

  83. hotels说道:

    This web site definitely has all the info I wanted about this subject and didn at know who to ask.

  84. Very nice post. I just stumbled upon your weblog and wished to say that I have truly enjoyed browsing your blog posts. After all I will be subscribing to your rss feed and I hope you write again soon!

  85. Really appreciate you sharing this blog.Really looking forward to read more. Much obliged.

  86. We stumbled over here by a different website and thought I might check things out. I like what I see so now i am following you. Look forward to finding out about your web page again.

  87. Major thankies for the post. Keep writing.

  88. personal injury说道:

    Wow, amazing weblog format! How lengthy have you been blogging for?

  89. what you have beаА аЂа•n dаА аБТ–аА аЂа•aming of.

  90. Very nice post. I just stumbled upon your blog and wanted to say that I ave truly enjoyed browsing your blog posts. In any case I all be subscribing to your feed and I hope you write again very soon!

  91. Chapel说道:

    Wow! This could be one particular of the most beneficial blogs We have ever arrive across on this subject. Actually Excellent. I am also an expert in this topic so I can understand your effort.

  92. take a look at说道:

    information with us. Please keep us up to date like this.

  93. Wow, fantastic weblog structure! How long have you been running a blog for? you made blogging glance easy. The entire look of your website is excellent, let alone the content!

  94. Some truly great blog posts on this web site , thanks for contribution.

  95. Maybe you can write subsequent articles relating to this

  96. just beneath, are a lot of completely not connected websites to ours, on the other hand, they are certainly worth going over

  97. wow, awesome blog post.Really looking forward to read more. Want more.

  98. There is clearly a lot to realize about this. I assume you made various nice points in features also.

  99. Really appreciate you sharing this post. Awesome.

  100. facebook说道:

    Thanks for sharing this great post. Very interesting ideas! (as always, btw)

  101. take a look at说道:

    I truly appreciate this article post.Much thanks again. Cool.

  102. wow, awesome post.Really thank you! Cool.

  103. miller electric说道:

    Some really choice content on this site, saved to my bookmarks.

  104. app说道:

    Wonderful items from you, man. I ave bear in mind your stuff prior to and you are

  105. This is one awesome blog article.Thanks Again.

  106. This blog is obviously educating and also factual. I have discovered helluva useful stuff out of this blog. I ad love to go back every once in a while. Cheers!

  107. Keep up the superb piece of work, I read few articles on this web site and I conceive that your site is very interesting and has lots of superb info.

  108. Rattling great info can be found on website.

  109. I truly appreciate this blog article.Thanks Again. Want more.

  110. Claudia Casabona说道:

    Hiya! I simply wish to give a huge thumbs up for the good info you have here on this post. I can be coming back to your weblog for more soon.

  111. visit说道:

    Wonderful article! We will be linking to this particularly great article on our website. Keep up the good writing.

  112. Muchos Gracias for your blog.Much thanks again. Want more.

  113. I really liked your blog article. Much obliged.

  114. check here说道:

    This awesome blog is obviously interesting and informative. I have found a lot of helpful advices out of this source. I ad love to return again and again. Cheers!

  115. go there说道:

    Roulette Table Planer! Bygg din egen Casino!

  116. Looking forward to reading more. Great blog post.Really thank you! Will read on

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>