Chinaunix首页 | 论坛 | 博客
  • 博客访问: 346870
  • 博文数量: 81
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 847
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-25 22:29
个人简介

执一不失,能君万物http://weidian.com/s/284520723?wfr=c

文章分类

全部博文(81)

文章存档

2016年(11)

2015年(70)

我的朋友

分类: LINUX

2015-07-07 22:58:06


点击(此处)折叠或打开

  1. 1.sort的工作原理
  2. sort将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出。
  3. [root@localhost ~]# cat sort.txt
  4. d
  5. c
  6. b
  7. a
  8. [root@localhost ~]# sort sort.txt
  9. a
  10. b
  11. c
  12. d
  13. [root@localhost ~]#
  14. 2.sort的-u选项
  15. [root@localhost ~]# cat sort.txt
  16. d
  17. c
  18. c
  19. b
  20. b
  21. a
  22. [root@localhost ~]# sort sort.txt
  23. a
  24. b
  25. b
  26. c
  27. c
  28. d
  29. [root@localhost ~]# sort -u sort.txt
  30. a
  31. b
  32. c
  33. d
  34. [root@localhost ~]#
  35. 3.sort的-r选项
  36. ort默认的排序方式是升序,如果想改成降序,就加个-r(reverse)就搞定了。
  37. [root@localhost ~]# cat sort.txt
  38. d
  39. c
  40. b
  41. e
  42. g
  43. f
  44. b
  45. a
  46. [root@localhost ~]# sort -r sort.txt
  47. g
  48. f
  49. e
  50. d
  51. c
  52. b
  53. b
  54. a
  55. [root@localhost ~]# sort -ru sort.txt
  56. g
  57. f
  58. e
  59. d
  60. c
  61. b
  62. a
  63. [root@localhost ~]#
  64. 4.sort的-o选项
  65. 由于sort默认是把结果输出到标准输出,所以需要用重定向才能将结果写入文件,形如sort filename > newfile。
  66. 但是,如果你想把排序结果输出到原文件中,用重定向可就不行了。o选项出现了,它成功的解决了这个问题,让你放心的将结果写入原文件。这或许也是-o比重定向的唯一优势所在。
  67. [root@localhost ~]# cat sort.txt
  68. d
  69. c
  70. b
  71. e
  72. g
  73. f
  74. b
  75. a
  76. [root@localhost ~]# sort -ur sort.txt -o sort.txt
  77. [root@localhost ~]# cat sort.txt
  78. g
  79. f
  80. e
  81. d
  82. c
  83. b
  84. a
  85. [root@localhost ~]#
  86. 5.sort的-n选项
  87. 使用-n选项,来告诉sort,“要以数值来排序”!
  88. [root@localhost ~]# cat num.txt
  89. 22
  90. 4
  91. 11
  92. 3
  93. 55
  94. 9
  95. [root@localhost ~]# sort num.txt
  96. 11
  97. 22
  98. 3
  99. 4
  100. 55
  101. 9
  102. [root@localhost ~]# sort -n num.txt
  103. 3
  104. 4
  105. 9
  106. 11
  107. 22
  108. 55
  109. [root@localhost ~]#
  110. 6.sort的-t选项和-k选项
  111. sort提供了-t选项,后面可以设定间隔符;指定了间隔符之后,就可以用-k来指定列数了。
  112. [root@localhost ~]# cat fruit.txt
  113. apple:10:2.2
  114. orange:4:4.4
  115. banana:11:3.3
  116. [root@localhost ~]# sort -n -k 2 -t: fruit.txt
  117. orange:4:4.4
  118. apple:10:2.2
  119. banana:11:3.3
  120. [root@localhost ~]# sort -n -k 3 -t: fruit.txt
  121. apple:10:2.2
  122. banana:11:3.3
  123. orange:4:4.4
  124. [root@localhost ~]#
  125. 7.其他选项
  126. -f会将小写字母都转换为大写字母来进行比较,亦即忽略大小写

  127. -c会检查文件是否已排好序,如果乱序,则输出第一个乱序的行的相关信息,最后返回1

  128. -C会检查文件是否已排好序,如果乱序,不输出内容,仅返回1

  129. -M会以月份来排序,比如JAN小于FEB等等

  130. -b会忽略每一行前面的所有空白部分,从第一个可见字符开始比较。
  131. [root@localhost ~]# cat fruit.txt
  132. apple:10:2.2
  133. orange:4:4.4
  134. banana:11:5.3
  135. pear:11:4.4
  136. [root@localhost ~]# sort -t: -k 2n -k 3n fruit.txt
  137. orange:4:4.4
  138. apple:10:2.2
  139. pear:11:4.4
  140. banana:11:5.3
  141. [root@localhost ~]#

  142. [ FStart [ .CStart ] ] [ Modifier ] [ , [ FEnd [ .CEnd ] ][ Modifier ] ]

  143. 这个语法格式可以被其中的逗号(“,”)分为两大部分,Start部分和End部分。

  144. 先给你灌输一个思想,那就是“如果不设定End部分,那么就认为End被设定为行尾”。这个概念很重要的,但往往你不会重视它。

  145. Start部分也由三部分组成,其中的Modifier部分就是我们之前说过的类似n和r的选项部分。我们重点说说Start部分的FStart和C.Start。

  146. C.Start也是可以省略的,省略的话就表示从本域的开头部分开始。之前例子中的-k 2和-k 3就是省略了C.Start的例子喽。

  147. FStart.CStart,其中FStart就是表示使用的域,而CStart则表示在FStart域中从第几个字符开始算“排序首字符”。

  148. 同理,在End部分中,你可以设定FEnd.CEnd,如果你省略.CEnd,则表示结尾到“域尾”,即本域的最后一个字符。或者,如果你将CEnd设定为0(零),也是表示结尾到“域尾”。

  149. [root@localhost ~]# sort -t: -k 1.3 fruit.txt
  150. orange:4:4.4
  151. pear:11:4.4
  152. banana:11:5.3
  153. apple:10:2.2
  154. 从英文名称的第3个字母开始进行排序:
  155. [root@localhost ~]# cat fruit.txt
  156. apple:10:2.2
  157. orange:4:4.4
  158. banana:11:5.3
  159. pear:11:4.4
  160. [root@localhost ~]# sort -t: -k 1.3,1.3 -k 3,3nr fruit.txt
  161. orange:4:4.4
  162. pear:11:4.4
  163. banana:11:5.3
  164. apple:10:2.2
  165. [root@localhost ~]#
  166. 由于只对第3个字母进行排序,所以我们使用了-k 1.3,1.3的表示方式,表示我们“只”对第二个字母进行排序。(如果你问“我使用-k 1.3怎么不行?”,当然不行,因为你省略了End部分,这就意味着你将对从第二个字母起到本域最后一个字符为止的字符串进行排序)。对于员工工资进行排 序,我们也使用了-k 3,3,这是最准确的表述,表示我们“只”对本域进行排序,因为如果你省略了后面的3,就变成了我们“对第3个域开始到最后一个域位置的内容进行排序” 了。
  167. !!!!
  168. 在modifier部分还可以用到哪些选项?
  169. 可以用到b、d、f、i、n 或 r。

  170. 其中n和r你肯定已经很熟悉了。

  171. b表示忽略本域的签到空白符号。

  172. d表示对本域按照字典顺序排序(即,只考虑空白和字母)。

  173. f表示对本域忽略大小写进行排序。

  174. i表示忽略“不可打印字符”,只针对可打印字符进行排序。(有些ASCII就是不可打印字符,比如\a是报警,\b是退格,\n是换行,\r是回车等等)

  175. 设置了两层排序优先级的情况下,使用-u就没有删除任何行。原来-u是会权衡所有-k选项,将都相同的才会删除,只要其中有一级不同都不会轻易删除的:
  176. [root@localhost ~]# sort -t: -k 2n -u fruit.txt
  177. orange:4:4.4
  178. apple:10:2.2
  179. banana:11:5.3
  180. [root@localhost ~]# sort -t: -k1.3,1.3 -u fruit.txt
  181. orange:4:4.4
  182. banana:11:5.3
  183. apple:10:2.2
  184. [root@localhost ~]#
  185. [root@localhost ~]# sort -t: -k1.3,1.3 -k 3nr fruit.txt
  186. orange:4:4.4
  187. pear:11:4.4
  188. banana:11:5.3
  189. apple:10:2.2
  190. [root@localhost ~]#
  191. ---------------------------------
  192. [root@localhost ~]# sort -n -k 2.1,3.1 -k 3r fruit.txt
  193. apple:10:2.2
  194. banana:11:5.3
  195. orange:4:4.4
  196. pear:11:4.4
  197. [root@localhost ~]#
  198. ---------------------------------

阅读(1383) | 评论(0) | 转发(0) |
0

上一篇:awk的使用

下一篇:合并与排序2-uniq

给主人留下些什么吧!~~