shell腳本--awk數組實現去除重複行

去除重複行的方法有很多,這裏介紹三種。

測試文本:

[root@172-0-10-222 myscripts]# cat testfile
andy 123456
hanna 123456
hello world
welcome fuck
andy 123456
hello world
andy andy

這其中,有andy 123456和hello world是重複的。

(1)使用sort、uniq命令去除重複行

[root@172-0-10-222 myscripts]# cat testfile | sort | uniq
andy 123456
andy andy
hanna 123456
hello world
welcome fuck

這裏是先將每一行進行默認規則排序,然後把重複的行去掉。

(2)使用awk數組去除重複行

[root@172-0-10-222 myscripts]# cat testfile | awk '!arr[$0]++{print $0}'
andy 123456
hanna 123456
hello world
welcome fuck
andy andy

也可以簡寫成 cat testfile | awk '!arr[$0]++'

分析:將每一行數據作爲數組的下標,某下標x第一次出現的時候arr[x]爲0,第二次,第三次,。。。,第n次出現的時候arr[x]不爲0。這裏,!arr[$0]++是選擇第一次出現的行進行打印輸出。

(3)使用awk數組去除重複行詳細寫法

[root@172-0-10-222 myscripts]# cat testfile | awk '{arr[$0]=1}END{for(i in arr){print i}}'
welcome fuck
hanna 123456
andy 123456
hello world
andy andy

分析:以每一行作爲數組下標給數組賦值,重複行下標就會替換掉前面的下標。然後輸出留下來的下標即可。

 

案例:去除重複號碼行

號碼文件

[root@172-0-10-222 myscripts]# cat testfile
andy 15871731153
hanna 15387876543
hello 15578765389
welcome 15871731153
andy 13987273647
hello 15871731153
andy 15871731153

去除文件中號碼重複的行

[root@172-0-10-222 myscripts]# cat testfile | awk '!arr[$2]++'
andy 15871731153
hanna 15387876543
hello 15578765389
andy 13987273647

 

發佈了71 篇原創文章 · 獲贊 5 · 訪問量 6454
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章