Linux/Unix排序、合并與分割 排序:sort[oracle@localhost sort.folder]$ sort -c ls.out sort: ls.out:2: disorder: -rw-r--r-- 1 oracle oinstall 2653 Feb 23 21:30 awk.out www.2cto.com 表示未分類,使用 sort ls.out > ls.sort 將之分類重試:[oracle@localhost sort.folder]$ sort ls.out > ls.sort[oracle@localhost sort.folder]$ sort -c ls.sort -k5表示根據第5行按降序排列:[oracle@localhost sort.folder]$ sort -k 5 ls.out total 80-rw-r--r-- 1 oracle oinstall 0 Feb 24 14:21 ls.out-rw-r--r-- 1 oracle oinstall 0 Feb 24 14:21 sed.sh-rwxr-xr-x 1 oracle oinstall 26 Nov 10 22:30 main.sh-rwxrwxrwx 1 oracle oinstall 44 Jan 7 22:39 shelltest.sh-rwxr-xr-x 1 oracle oinstall 99 Feb 23 11:51 test1.sh-rwxr-xr-x 1 oracle oinstall 99 Feb 23 11:52 test2.sh-rw-r--r-- 1 oracle oinstall 216 Feb 23 11:53 blank_file-rwxr--r-- 1 oracle oinstall 642 Feb 15 18:43 find.sh-rwxr--r-- 1 oracle oinstall 2250 Feb 23 21:20 awk.sh-rw-r--r-- 1 oracle oinstall 2653 Feb 23 21:30 awk.outdrwxr-xr-x 2 oracle oinstall 4096 Feb 15 17:56 folder www.2cto.com -k5表示根據第5行按降升序排列:[oracle@localhost sort.folder]$ sort -k5 -r ls.out drwxr-xr-x 2 oracle oinstall 4096 Feb 15 17:56 folder-rw-r--r-- 1 oracle oinstall 2653 Feb 23 21:30 awk.out-rwxr--r-- 1 oracle oinstall 2250 Feb 23 21:20 awk.sh-rwxr--r-- 1 oracle oinstall 642 Feb 15 18:43 find.sh-rw-r--r-- 1 oracle oinstall 216 Feb 23 11:53 blank_file-rwxr-xr-x 1 oracle oinstall 99 Feb 23 11:52 test2.sh-rwxr-xr-x 1 oracle oinstall 99 Feb 23 11:51 test1.sh-rwxrwxrwx 1 oracle oinstall 44 Jan 7 22:39 shelltest.sh-rwxr-xr-x 1 oracle oinstall 26 Nov 10 22:30 main.sh-rw-r--r-- 1 oracle oinstall 0 Feb 24 14:21 sed.sh-rw-r--r-- 1 oracle oinstall 0 Feb 24 14:21 ls.outtotal 80 返回排序後的第一行:[oracle@localhost sort.folder]$ sort -k5 -r ls.out | head -1drwxr-xr-x 2 oracle oinstall 4096 Feb 15 17:56 folder 返回排序後的最後兩行:[oracle@localhost sort.folder]$ sort -k5 -r ls.out | tail -2-rw-r--r-- 1 oracle oinstall 0 Feb 24 14:21 ls.outtotal 80 awk使用sort的輸出結果:[oracle@localhost sort.folder]$ sort -k5 -r ls.out | head -2 | awk '{print "file_name:"$9 " size:"$5}' file_name:folder size:4096file_name:awk.out size:2653 某個檔案中的內容如下:[oracle@localhost sort.folder]$ cat iplist 193.132.80.123 dave tansley193.132.80.23 HP printer 2nd floor193.132.80.198 JJ. Peter's scanner193.132.80.38 SPARE193.132.80.78 P.Edron需要根據ip地址的最後一段進行排序:[oracle@localhost sort.folder]$ sort -t. -k4 -r -n iplist193.132.80.198 JJ. Peter's scanner193.132.80.123 dave tansley193.132.80.78 P.Edron193.132.80.38 SPARE193.132.80.23 HP printer 2nd floor這裡-r降序,-t.表示根據.進行分類,-n表示排序的是數實值型別 uniq去重複用法:其選項含義:-u 只顯示不重複行。-d 只顯示有重複資料行,每種重複行只顯示其中一行-c 列印每一重複行出現次數。-f n為數字,前n個域被忽略。 對已經排序的檔案內容進行去重操作:[oracle@localhost sort.folder]$ cat ls.sort total 80total 80-rw-r--r-- 1 oracle oinstall 0 Feb 24 14:21 ls.out-rw-r--r-- 1 oracle oinstall 0 Feb 24 14:21 ls.out-rw-r--r-- 1 oracle oinstall 0 Feb 24 14:21 sed.sh-rw-r--r-- 1 oracle oinstall 0 Feb 24 14:21 sed.sh-rwxr-xr-x 1 oracle oinstall 26 Nov 10 22:30 main.sh-rwxr-xr-x 1 oracle oinstall 26 Nov 10 22:30 main.sh-rwxrwxrwx 1 oracle oinstall 44 Jan 7 22:39 shelltest.sh-rwxrwxrwx 1 oracle oinstall 44 Jan 7 22:39 shelltest.sh-rwxr-xr-x 1 oracle oinstall 99 Feb 23 11:51 test1.sh-rwxr-xr-x 1 oracle oinstall 99 Feb 23 11:51 test1.sh-rwxr-xr-x 1 oracle oinstall 99 Feb 23 11:52 test2.sh-rwxr-xr-x 1 oracle oinstall 99 Feb 23 11:52 test2.sh-rw-r--r-- 1 oracle oinstall 216 Feb 23 11:53 blank_file-rw-r--r-- 1 oracle oinstall 216 Feb 23 11:53 blank_file-rwxr--r-- 1 oracle oinstall 642 Feb 15 18:43 find.sh-rwxr--r-- 1 oracle oinstall 642 Feb 15 18:43 find.sh-rwxr--r-- 1 oracle oinstall 2250 Feb 23 21:20 awk.sh-rwxr--r-- 1 oracle oinstall 2250 Feb 23 21:20 awk.sh-rw-r--r-- 1 oracle oinstall 2653 Feb 23 21:30 awk.out-rw-r--r-- 1 oracle oinstall 2653 Feb 23 21:30 awk.outdrwxr-xr-x 2 oracle oinstall 4096 Feb 15 17:56 folderdrwxr-xr-x 2 oracle oinstall 4096 Feb 15 17:56 folder [oracle@localhost sort.folder]$ uniq ls.sort total 80-rw-r--r-- 1 oracle oinstall 0 Feb 24 14:21 ls.out-rw-r--r-- 1 oracle oinstall 0 Feb 24 14:21 sed.sh-rwxr-xr-x 1 oracle oinstall 26 Nov 10 22:30 main.sh-rwxrwxrwx 1 oracle oinstall 44 Jan 7 22:39 shelltest.sh-rwxr-xr-x 1 oracle oinstall 99 Feb 23 11:51 test1.sh-rwxr-xr-x 1 oracle oinstall 99 Feb 23 11:52 test2.sh-rw-r--r-- 1 oracle oinstall 216 Feb 23 11:53 blank_file-rwxr--r-- 1 oracle oinstall 642 Feb 15 18:43 find.sh-rwxr--r-- 1 oracle oinstall 2250 Feb 23 21:20 awk.sh-rw-r--r-- 1 oracle oinstall 2653 Feb 23 21:30 awk.outdrwxr-xr-x 2 oracle oinstall 4096 Feb 15 17:56 folder 顯示重複的次數:[oracle@localhost sort.folder]$ uniq -dc ls.sort 2 total 80 2 -rw-r--r-- 1 oracle oinstall 0 Feb 24 14:21 ls.out 2 -rw-r--r-- 1 oracle oinstall 0 Feb 24 14:21 sed.sh 2 -rwxr-xr-x 1 oracle oinstall 26 Nov 10 22:30 main.sh 2 -rwxrwxrwx 1 oracle oinstall 44 Jan 7 22:39 shelltest.sh 2 -rwxr-xr-x 1 oracle oinstall 99 Feb 23 11:51 test1.sh 2 -rwxr-xr-x 1 oracle oinstall 99 Feb 23 11:52 test2.sh 2 -rw-r--r-- 1 oracle oinstall 216 Feb 23 11:53 blank_file 2 -rwxr--r-- 1 oracle oinstall 642 Feb 15 18:43 find.sh 2 -rwxr--r-- 1 oracle oinstall 2250 Feb 23 21:20 awk.sh 2 -rw-r--r-- 1 oracle oinstall 2653 Feb 23 21:30 awk.out 2 drwxr-xr-x 2 oracle oinstall 4096 Feb 15 17:56 folder 下面ls.sort2檔案中有如下內容:[oracle@localhost sort.folder]$ cat ls.sort2 0 oracle oinstall0 oracle oinstall0 oracle oinstall0 oracle oinstall26 oracle oinstall26 oracle oinstall44 oracle oinstall44 oracle oinstall99 oracle oinstall99 oracle oinstall99 oracle oinstall99 oracle oinstall216 oracle oinstall216 oracle oinstall642 oracle oinstall642 oracle oinstall2250 oracle oinstall2250 oracle oinstall2653 oracle oinstall2653 oracle oinstall4096 oracle oinstall4096 oracle oinstall 去重複顯示:[oracle@localhost sort.folder]$ uniq -c ls.sort2 4 0 oracle oinstall 2 26 oracle oinstall 2 44 oracle oinstall 4 99 oracle oinstall 2 216 oracle oinstall 2 642 oracle oinstall 2 2250 oracle oinstall 2 2653 oracle oinstall 2 4096 oracle oinstall 使用-f前2個域忽略,則系統判斷均為重複資料:[oracle@localhost sort.folder]$ uniq -cf2 ls.sort2 22 0 oracle oinstall join的用法:有兩個檔案,內容如下:[oracle@localhost sort.folder]$ cat names.txt M.Golls 12 Hidd RdP.Heller The AcreP.Willey 132 The GroveT.Norms 84 Connaught RdK.Fletch 12 Woodlea[oracle@localhost sort.folder]$ cat town.txt M.Golls Norwith NRDP.Willey Galashiels GDDT.Norms Brandon BSLK.Fletch Mildenhall MAF 我們來串連兩個檔案(其實類似於sql中的join):[oracle@localhost sort.folder]$ join names.txt town.txt M.Golls 12 Hidd Rd Norwith NRDP.Willey 132 The Grove Galashiels GDDT.Norms 84 Connaught Rd Brandon BSLK.Fletch 12 Woodlea Mildenhall MAF 顯示第一個檔案中未與第二個檔案內容匹配的行:[oracle@localhost sort.folder]$ join -a1 names.txt town.txt M.Golls 12 Hidd Rd Norwith NRDP.Heller The AcreP.Willey 132 The Grove Galashiels GDDT.Norms 84 Connaught Rd Brandon BSLK.Fletch 12 Woodlea Mildenhall MAF 顯示第一個檔案的第一個域和第二個檔案的第二個域:[oracle@localhost sort.folder]$ join -o 1.1,2.2 names.txt town.txt M.Golls NorwithP.Willey GalashielsT.Norms BrandonK.Fletch Mildenhall 將檔案1的域1和檔案2的域1作為串連鍵:[oracle@localhost sort.folder]$ join -j1 1 -j2 1 names.txt town.txt M.Golls 12 Hidd Rd Norwith NRDP.Willey 132 The Grove Galashiels GDDT.Norms 84 Connaught Rd Brandon BSLK.Fletch 12 Woodlea Mildenhall MAF cut用來剪下從標準輸入或者文本中的列或者域。剪下的文本可以粘貼到文字檔中:剪下以點號分割的第三個域:[oracle@localhost sort.folder]$ cut -d. -f3 iplist 8080808080 paste粘貼:[oracle@localhost sort.folder]$ ls | paste -d"" -iplistls.outls.sortls.sort2names.txtpaste1paste2sort.shtown.txt 將兩個檔案分為兩列粘貼,以冒號隔開:[oracle@localhost sort.folder]$ paste -d: names.txt town.txt M.Golls 12 Hidd Rd:M.Golls Norwith NRDP.Heller The Acre:P.Willey Galashiels GDDP.Willey 132 The Grove:T.Norms Brandon BSLT.Norms 84 Connaught Rd:K.Fletch Mildenhall MAFK.Fletch 12 Woodlea: split分割檔案:檔案較大時可以使用此方法:[oracle@localhost sort.folder]$ lltotal 64-rw-r--r-- 1 oracle oinstall 143 Mar 3 10:58 iplist-rw-r--r-- 1 oracle oinstall 620 Mar 3 10:51 ls.out-rw-r--r-- 1 oracle oinstall 1240 Mar 3 11:14 ls.sort-rw-r--r-- 1 oracle oinstall 436 Mar 3 11:25 ls.sort2-rw-r--r-- 1 oracle oinstall 104 Mar 3 11:33 names.txt-rw-r--r-- 1 oracle oinstall 12 Mar 3 12:07 paste1-rw-r--r-- 1 oracle oinstall 0 Mar 3 12:04 paste2-rwxr--r-- 1 oracle oinstall 0 Mar 3 09:51 sort.sh-rw-r--r-- 1 oracle oinstall 88 Mar 3 11:35 town.txt 將檔案分割為兩行一個檔案:[oracle@localhost sort.folder]$ split -2 ls.out [oracle@localhost sort.folder]$ lltotal 112-rw-r--r-- 1 oracle oinstall 143 Mar 3 10:58 iplist-rw-r--r-- 1 oracle oinstall 620 Mar 3 10:51 ls.out-rw-r--r-- 1 oracle oinstall 1240 Mar 3 11:14 ls.sort-rw-r--r-- 1 oracle oinstall 436 Mar 3 11:25 ls.sort2-rw-r--r-- 1 oracle oinstall 104 Mar 3 11:33 names.txt-rw-r--r-- 1 oracle oinstall 12 Mar 3 12:07 paste1-rw-r--r-- 1 oracle oinstall 0 Mar 3 12:04 paste2-rwxr--r-- 1 oracle oinstall 0 Mar 3 09:51 sort.sh-rw-r--r-- 1 oracle oinstall 88 Mar 3 11:35 town.txt-rw-r--r-- 1 oracle oinstall 108 Mar 3 12:14 xaa-rw-r--r-- 1 oracle oinstall 113 Mar 3 12:14 xab-rw-r--r-- 1 oracle oinstall 109 Mar 3 12:14 xac-rw-r--r-- 1 oracle oinstall 111 Mar 3 12:14 xad-rw-r--r-- 1 oracle oinstall 116 Mar 3 12:14 xae-rw-r--r-- 1 oracle oinstall 63 Mar 3 12:14 xaf