天天看點

linux sort指令學習

linux sort指令以行為機關對文本檔案進行排序。

接下來我們會以/tmp/sort_test.txt這個文本檔案為例對sort指令的用法進行說明。

sh-# cat /tmp/sort_test.txt

10 my name is xulin

2 I like programming

3 what about you?

sh-#

1. 因為sort指令是按照字元比較的方式進行排序,是以便出現以下的結果,

sh-# cat /tmp/sort_test.txt | sort

2. 指定-n選項,這樣就會以字元串數值大小進行排序,

sh-# cat /tmp/sort_test.txt | sort -n

3. 如果要反向排序,那就要指定-r選項了,

sh-# cat /tmp/sort_test.txt | sort -nr

4. 有時候使用者希望能夠按字段進行排序,其中-t選項用來指定域分隔符,-k用來指定位置,

sh-# cat /tmp/sort_test.txt | sort -t' ' -k 1,2

sh-# cat /tmp/sort_test.txt | sort -t' ' -k 2,3

sh-# ifconfig lo

lo        Link encap:Local Loopback

          inet addr:127.0.0.1  Mask:255.0.0.0

          UP LOOPBACK RUNNING  MTU:16436  Metric:1

          RX packets:9 errors:0 dropped:0 overruns:0 frame:0

          TX packets:9 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0

          RX bytes:456 (456.0 b)  TX bytes:456 (456.0 b)

sh-#

sh-# ifconfig lo | sort

          RX bytes:456 (456.0 b)  TX bytes:456 (456.0 b)

sh-# ifconfig lo | sort -t: -k 2 -n

          collisions:0 txqueuelen:0

sh-# ifconfig lo | sort -t: -k 2 -nr

繼續閱讀