linux sort命令

來源:互聯網
上載者:User

標籤:c   style   class   blog   code   java   

1、sort將檔案的每一行作為一個單位,相互比較,比較原則是從首字元向後,依次按ASCII碼值進行比較,最後將他們按升序輸出。

?
1 2 3 4 5 6 7 8 9 10 [[email protected] ~]# cat seq.txt  banana apple pear orange [[email protected] ~]# sort seq.txt   #預設升序 apple banana orange pear

2、sort -u 選項---去除重複行

?
1 2 3 4 5 6 7 8 9 10 11 12 13 [[email protected] ~]# cat seq.txt banana apple pear orange pear   [[email protected] ~]# sort -u seq.txt   apple banana orange pear

3、sort -r 選項----降序  

?
1 2 3 4 5 6 7 8 9 10 11 [[email protected] ~]# sort -u seq.txt   apple banana orange pear [[email protected] ~]# sort -ur seq.txt pear orange banana apple

4、sort -o選項----重新導向,在linux下與 >相同

[[email protected] ~]# cat seqNO.txt 12345[[email protected] ~]# sort -r seqNO.txt > number.txt[[email protected] ~]# cat number.txt 54321[[email protected] ~]# sort -r seqNO.txt -o number2.txt[[email protected] ~]# cat number2.txt 54321

5、sort -n 選項---以數值排序 

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 [[email protected] ~]# cat seq2    #排序程式將這些數字按字元來排序了,排序程式會先比較1和2,所以下面的升序中1反而最大。 1 2 5 8 10 11 100 [[email protected] ~]# sort seq2                                                                                          1 10 100 11 2 5 8 [[email protected] ~]# sort -r seq2 8 5 2 11 100 10 1 [[email protected] ~]# sort -n seq2  #-n以數值排序 1 2 5 8 10 11 100 [[email protected] ~]# sort -nr seq2 100 11 10 8 5 2 1 [[email protected] ~]# cat seq2 1 2 5 8 10 11 100 apple banana peal orange [[email protected] ~]# sort -n seq2  #當排序檔案有字元和數字,先排序字元,再排序數字 apple banana orange peal 1 2 5 8 10 11 100 [[email protected] ~]# sort -r seq2 peal orange banana apple 8 5 2 11 100 10 1 [[email protected] ~]# sort -rn seq2 100 11 10 8 5 2 1 peal orange banana apple

6、sort -t -k選項---t指定分隔字元,-k指定以哪一列進行排序

?
1 2 3 4 5 6 7 8 9 10 [[email protected] ~]# cat score  #共三列,假使:第一列表示姓名,第二列表示數學分數,第三列表示英語分數 Rusky,80, 90 Jack ,90, 100 Anna ,75, 80 Tom  ,88, 70 [[email protected] ~]# sort -n -k 2 -t , score   #以第二列數學分數重新排序,分隔字元為逗號 Anna ,75, 80 Rusky,80, 90 Tom  ,88, 70 Jack ,90, 100

  [[email protected] ~]# sort -n -k 2 -t ‘ ‘ score2    #如果分隔字元為空白格,則用單引號,中間加空格即可
  Anna 75 80
  Rusky 80 90
  Tom 88 70
  Jack 90 100

   [[email protected] ~]# sort -k 1 score
   Anna ,75, 80
   Jack ,90, 100
   Rusky,80, 90
   Tom ,88, 70

7 其他的sort常用選項

-f會將小寫字母都轉換為大寫字母來進行比較,亦即忽略大小寫

-c會檢查檔案是否已排好序,如果亂序,則輸出第一個亂序的行的相關資訊,最後返回1

-C會檢查檔案是否已排好序,如果亂序,不輸出內容,僅返回1

-M會以月份來排序,比如JAN小於FEB等等

-b會忽略每一行前面的所有空白部分,從第一個可見字元開始比較。

以數值,第二列進行排序,如果第二列有相同的,則以第三列進行排序[[email protected] ~]# cat score2Rusky 80  90Jack  90  100Anna  75  80Tom   80  70[[email protected] ~]# sort -n -k 2 -k 3 -t ‘ ‘ score2Anna  75  80Tom   80  70Rusky 80  90Jack  90  100
以第三列值逆序排序[[email protected] ~]# sort -t ‘ ‘ -k 3nr score2 Jack  90  100Rusky 80  90Tom   80  70Anna  75  80以第二列值逆序排序,如果相同值,則以第三列長升序排序[[email protected] ~]# sort -t ‘ ‘ -k 2nr -k 3n score2Jack  90  100Tom   80  70Rusky 80  90Anna  75  80

《FROM:http://www.cnblogs.com/51linux/archive/2012/05/23/2515299.html》 

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.