• 實用的分類(sort)操作。
sort命令的一般格式為:
sort -cmu -o output_file [other options] +pos1 +pos2 input_files
下面簡要介紹一下s o r t的參數:
-c 測試文件是否已經分類。
-m 合并兩個分類文件。
-u 刪除所有復制行。
-o 存儲s o r t結果的輸出文件名。
其他選項有:
-b 使用域進行分類時,忽略第一個空格。
-n 指定分類是域上的數字分類。
-t 域分隔符;用非空格或t a b鍵分隔域。
-r 對分類次序或比較求逆。
+n n為域號。使用此域號開始分類。
n n為域號。在分類比較時忽略此域,一般與+ n一起使用。
post1 傳遞到m,n。m為域號,n為開始分類字符數;例如4,6意即以第5域分類,從第7
個字符開始。
舉例:
按第一個域分類
-bash-3.00$ sort -k0 sed.txt
打印分類后的最后第一行
-bash-3.00$ sort -k0 sed.txt | tail -1
打印分類后的第一行
-bash-3.00$ sort -k0 sed.txt | head -1
awk使用sort輸出結果
-bash-3.00$ sort -k0 sed.txt | head -1 | awk '{if($1=="caodejun")print $1}'
將兩個分類文件合并
-bash-3.00$ sort -m sed.txt sort.txt
將文件合并前,它們必須已被分類。合并文件可用于事務處理和任何種類的修改操作。
下面這個例子,因為忘了把兩個家電名稱加入文件,它們被放在一個單獨的文件里,現在將
之并入一個文件。分類的合并格式為‘sort -m sorted_file1 sorted_file2’。
刪除重復行
-bash-3.00$ sort -u sed.txt
• uniq
uniq用來從一個文本文件中去除或禁止重復行。一般uniq假定文件已分類,并且結果正確。我們并不強制要求這樣做,如果愿意,可以使用任何非排序文本,甚至是無規律行。
-bash-3.00$ who | awk '{print $1} ' |uniq
liuzk423
605408211
shuzigui
nefu_luyanshen
waterlooz
wsoangel
tomotoboy
xp55699312
zyy0904
caodejun
duke1988
605408211
nefu_luyanshen
zyy0904
lonelysand
顯示不唯一的行
-bash-3.00$ who | awk '{print $1} ' |uniq -d
-c打印每一重復行出現次數。
-bash-3.00$ who | awk '{print $1} ' |uniq -c
1 liuzk423
1 605408211
1 shuzigui
1 nefu_luyanshen
1 waterlooz
1 wsoangel
1 tomotoboy
1 xp55699312
1 zyy0904
1 caodejun
1 duke1988
1 605408211
1 nefu_luyanshen
1 zyy0904
1 lonelysand
這里沒有搞懂nefu_luyanshen明明重復,卻顯示重復行數目為1
對特定域進行測試,使用-n只測試一行一部分的唯一性。
-bash-3.00$ who | awk '{print $1} ' |uniq -n2
liuzk423
• join
將兩個已經分好類的文件連接在一起哈。一些系統要求使用join時文件域要少于20,為公平起見,如果域大于20,應使用DBMS系統,其一般格式如下:
join [options] in_file1 in_file2
-bash-3.00$ cat sed.txt
605408211 pts/16 Jul 31 13:54 (218.0.1.42)
caodejun pts/44 Jul 31 14:16 (219.148.133.31)
duke1988 pts/45 Jul 31 14:41 (218.104.163.66)
liuzk423 pts/6 Jul 20 08:27 (219.245.104.240)
nefu_luyanshen pts/23 Jul 31 14:33 (218.25.6.142)
nefu_luyanshen pts/48 Jul 31 12:59 (218.25.6.142)
shuzigui pts/21 Jul 31 12:11 (121.35.248.193)
tomotoboy pts/41 Jul 31 13:31 (219.221.99.155)
waterlooz pts/25 Jul 31 08:48 (121.0.29.225)
wsoangel pts/35 Jul 31 13:40 (116.233.219.10)
xp55699312 pts/42 Jul 31 14:12 (61.152.132.103)
zyy0904 pts/43 Jul 31 13:53 (125.33.195.36)
-bash-3.00$ cat sort.txt
605408211 pts/16 Jul 31 13:54 (218.0.1.42)
caodejun pts/44 Jul 31 14:16 (219.148.133.31)
duke1988 pts/45 Jul 31 14:41 (218.104.163.66)
-bash-3.00$ join sed.txt sort.txt
605408211 pts/16 Jul 31 13:54 (218.0.1.42) pts/16 Jul 31 13:54 (218.0.1.42)
caodejun pts/44 Jul 31 14:16 (219.148.133.31) pts/44 Jul 31 14:16 (219.148.133.31)
duke1988 pts/45 Jul 31 14:41 (218.104.163.66) pts/45 Jul 31 14:41 (218.104.163.66)
選擇匹配
-bash-3.00$ join -a1 -a2 sed.txt sort.txt
605408211 pts/16 Jul 31 13:54 (218.0.1.42) pts/16 Jul 31 13:54 (218.0.1.42)
caodejun pts/44 Jul 31 14:16 (219.148.133.31) pts/44 Jul 31 14:16 (219.148.133.31)
duke1988 pts/45 Jul 31 14:41 (218.104.163.66) pts/45 Jul 31 14:41 (218.104.163.66)
liuzk423 pts/6 Jul 20 08:27 (219.245.104.240)
nefu_luyanshen pts/23 Jul 31 14:33 (218.25.6.142)
nefu_luyanshen pts/48 Jul 31 12:59 (218.25.6.142)
shuzigui pts/21 Jul 31 12:11 (121.35.248.193)
tomotoboy pts/41 Jul 31 13:31 (219.221.99.155)
waterlooz pts/25 Jul 31 08:48 (121.0.29.225)
wsoangel pts/35 Jul 31 13:40 (116.233.219.10)
xp55699312 pts/42 Jul 31 14:12 (61.152.132.103)
zyy0904 pts/43 Jul 31 13:53 (125.33.195.36)
-bash-3.00$ join -o 1.1 2.2 sed.txt sort.txt
605408211 pts/16
caodejun pts/44
duke1988 pts/45
-bash-3.00$ join -o 1.1 2.2 2.3 sed.txt sort.txt
605408211 pts/16 Jul
caodejun pts/44 Jul
duke1988 pts/45 Jul
• cut
cut用來從標準輸入或文本文件中剪切列或域。剪切文本可以將之粘貼到一個文本文件。
下一節將介紹粘貼用法。
cut一般格式為:
cut [options] file1 file2
下面介紹其可用選項:
-c list 指定剪切字符數。
-f field 指定剪切域數。
-d 指定與空格和t a b鍵不同的域分隔符。
- c用來指定剪切范圍,如下所示:
- c 1,5-7 剪切第1個字符,然后是第5到第7個字符。
-c1-50 剪切前5 0個字符。
-f 格式與- c相同。
-f 1,5 剪切 第1域,第5域。
- f 1,10-12 剪切第1域,第1 0域到第1 2域。
-bash-3.00$ ps -ef | cut -c1-8
-bash-3.00$ ps -ef | cut -d: -f1
-bash-3.00$ ps -ef | cut -d: -f1,3
• paste
cut用來從文本文件或標準輸出中抽取數據列或者域,然后再用 paste可以將這些數據粘貼
起來形成相關文件。粘貼兩個不同來源的數據時,首先需將其分類,并確保兩個文件行數相
同。
paste將按行將不同文件行信息放在一行。缺省情況下, paste連接時,用空格或tab鍵分隔
新行中不同文本,除非指定- d選項,它將成為域分隔符。paste格式為:
paste -d -s -file1 file2
選項含義如下:
-d 指定不同于空格或t a b鍵的域分隔符。例如用@分隔域,使用- d @。
-s 將每個文件合并成行而不是按行粘貼。
- 使用標準輸入。例如ls -l |paste ,意即只在一列上顯示輸出。
• split
split用來將大文件分割成小文件。有時文件越來越大,傳送這些文件時,首先將其分割可
能更容易。使用v i或其他工具諸如sort時,如果文件對于工作緩沖區太大,也會存在一些問題。
因此有時沒有選擇余地,必須將文件分割成小的碎片。
split命令一般格式:
split -output_file-size input-filename output-filename
這里output_file-size 指的是文本文件被分割的行數。
-bash-3.00$ ps -ef |split -10
-bash-3.00$ ls
a.out greeting.sh main.c sort.txt xac xai xao
|
|
append.sed grepgrepstrings nohup.out test xad xaj xap
|
|
change.sed grepstr readme.sh test.sh xae xak xaq
|
|
core.log hello seawolf user.online xaf xal xar
|
|
factorial hello.cpp sed.out xaa xag xam
|
|
factorial.c main sed.txt xab xah xan |