2017-03-01 82 views
0

好了,所以我想刪除重複的行,但它是比這更復雜一點..與扭曲刪除重複行的GnuWin32

我有一個user.txt文件,命名,例如文件是:

users:[email protected] 
users1:[email protected] 

現在由於我的系統的人的錯誤都能夠使用相同的電子郵件作爲別人註冊了,所以我想,如果行有相同的電子郵件刪除不止一次,例如問題:

user:display:[email protected] 
user2:[email protected] 
user3:[email protected] 
user4:[email protected] 

公告浩W用戶,用戶2,用戶3,用戶4都有相同的電子郵件..以及我想刪除用戶2,用戶3,用戶4但保持用戶..反之亦然(第一個被請求接收)刪除任何其他行包含相同的電子郵件..

所以如果

[email protected] is in 20 lines remove 19 
[email protected] is in 555 lines remove 554 

等..第四

+1

使用電子郵件作爲'awk'數組中的索引。在處理每一行時,如果電子郵件不在數組中,請打印該行並將其添加到數組中。 – Barmar

+0

查看http://stackoverflow.com/questions/2604088/awk-remove-line-if-field-is-duplicate – Barmar

+0

你能解釋一下你的意思嗎?「第一個被請求接受」?你的標準究竟是選擇哪條線?按字母順序排列第一個用戶名?第一個出現在文件中? – Fred

回答

0

這可以用awk完成:

awk '!a["user:display:[email protected]"]++' filename 

++手段,轉向真實。所以,它匹配打印結果後。

!在這種情況下使用,以扭轉這種情況。所以比賽結束後,它變成了假。 (如不匹配後打印)

例如:

$ awk 'a["user:display:[email protected]"]++' filename 
user2:[email protected] 
user3:[email protected] 
user4:[email protected] 
line_random1 
linerandom_2_ 

現在用!

$ awk '!a["user:display:[email protected]"]++' filename 
user:display:[email protected] 

所以,現在你只需要過濾掉什麼awk上。不知道你的文件有多大,算至少我會做以下的條目:

$ grep -o '[email protected]' filename | wc -l 
4 

如果你知道該怎麼awk上,只是把它寫一個新的文件 - 只是爲了保存。

awk '!a["user:display:[email protected]"]++' filename >> new_filename