6,sort 命令
阿新 • • 發佈:2020-10-21
[root@centos7 ~]# cat b.txt | wc -l 13 [root@centos7 ~]# [root@centos7 ~]# cat b.txt 1 2 3 3 4 4 4 2 2 5 5 6 3 [root@centos7 ~]# cat b.txt |sort 1 2 2 2 3 3 3 4 4 4 5 5 6 [root@centos7 ~]# [root@centos7 ~]# uniq b.txt #uniq 預設對相鄰的內容去重 1 2 3 4 2 5 6 3 [root@centos7 ~]# uniq b.txt |sort 1 2 2 3 3 4 5 6 [root@centos7 ~]# [root@centos7 ~]# uniq -c b.txt 1 1 1 2 2 3 3 4 2 2 2 5 1 6 1 3 [root@centos7 ~]# uniq -c b.txt |sort 1 1 1 2 1 3 1 6 2 2 2 3 2 5 3 4 [root@centos7 ~]# a: 只對相鄰的相同內容去重 [root@centos7 ~]# cat c.txt 10.0.0.7 10.0.0.6 10.0.0.6 10.0.0.7 10.0.0.8 10.0.0.8 10.0.0.9 [root@centos7 ~]# [root@centos7 ~]# cat c.txt |wc -l 7 [root@centos7 ~]# [root@centos7 ~]# uniq c.txt 10.0.0.7 10.0.0.6 10.0.0.7 10.0.0.8 10.0.0.9 [root@centos7 ~]# 可通過sort配合完全去重;讓重複的行相鄰 [root@centos7 ~]# sort c.txt 10.0.0.6 10.0.0.6 10.0.0.7 10.0.0.7 10.0.0.8 10.0.0.8 10.0.0.9 [root@centos7 ~]# [root@centos7 ~]# sort c.txt |uniq 10.0.0.6 10.0.0.7 10.0.0.8 10.0.0.9 [root@centos7 ~]# [root@centos7 ~]# sort -u c.txt 10.0.0.6 10.0.0.7 10.0.0.8 10.0.0.9 [root@centos7 ~]# [root@centos7 ~]# awk -F '/' '{print $3}' oldboy.log|sort|uniq -c|sort -r # -r 倒排序 3 mp3.etiantian.org 2 post.etiantian.org 1 www.etiantian.org 將cc列數字從大到小順序排列: [root@centos7 ~]# cat b.txt eee:40:5.4 eee:60:5.1 ddd:20:4.2 ccc:50:3.3 bbb:10:2.5 aaa:30:1.6 [root@centos7 ~]# sort b.txt aaa:30:1.6 bbb:10:2.5 ccc:50:3.3 ddd:20:4.2 eee:40:5.4 eee:60:5.1 [root@centos7 ~]# sort -nrk 3 -t ':' b.txt eee:40:5.4 eee:60:5.1 ddd:20:4.2 ccc:50:3.3 bbb:10:2.5 aaa:30:1.6 [root@centos7 ~]# -n:依照數值的大小排序; -r:以相反的順序來排序; -t<分隔字元>:指定排序時所用的欄位分隔字元; -k:指定需要排序的欄位 -u :忽略相同行 [root@centos7 ~]# cat aa.txt 1 11 2 3 4 5 78 [root@centos7 ~]# [root@centos7 ~]# sort aa.txt 1 11 2 3 4 5 78 [root@centos7 ~]# sort -n aa.txt 1 2 3 4 5 11 78 [root@centos7 ~]# [root@centos7 ~]# cat aa.txt 1 a 11 b 2 c 3 d 4 e 5 k 78 y [root@centos7 ~]# [root@centos7 ~]# sort -t ' ' -k2 aa.txt 1 a 11 b 2 c 3 d 4 e 5 k 78 y [root@centos7 ~]# [root@centos7 ~]# awk 'BEGIN{array[1]="oldboy";array[2]="oldgirl";for(key in array) print key,array[key]}' 1 oldboy 2 oldgirl [root@centos7 ~]# 注意: awk 後面不加檔案,開頭必須要新增BEGIN [root@centos7 ~]# for i in 1 2 3 ;do echo $i ;done 1 2 3 [root@centos7 ~]#