删除vCard文件中的重复记录

时间:2018-06-09 16:28:49

标签: perl duplicates vcard

我有一个vCard文件,其中包含数千个联系人的记录。此文件已损坏,并为每个用户添加了个人电话,工作和额外记录的副本。

我怎样才能清理重复项?

BEGIN:VCARD
VERSION:3.0
N:Doe;John;Q.,Public
FN;CHARSET=UTF-8:John Doe
TEL;TYPE=WORK,VOICE:(111) 555-1212
TEL;TYPE=WORK,VOICE:(111) 555-1212
TEL;TYPE=WORK,VOICE:(111) 555-1212
TEL;TYPE=WORK,VOICE:(111) 555-1212
TEL;TYPE=HOME,VOICE:(404) 555-1212
TEL;TYPE=HOME,VOICE:(404) 555-1212
TEL;TYPE=HOME,VOICE:(404) 555-1212
TEL;TYPE=HOME,TYPE=VOICE:(404) 555-1213
TEL;TYPE=HOME,TYPE=VOICE:(404) 555-1213
TEL;TYPE=HOME,VOICE:(404) 555-1212
TEL;TYPE=HOME,VOICE:(404) 555-1212
TEL;TYPE=HOME,VOICE:(404) 555-1212
TEL;TYPE=HOME,TYPE=VOICE:(404) 555-1213
TEL;TYPE=HOME,TYPE=VOICE:(404) 555-1213
TEL;TYPE=HOME,TYPE=VOICE:(404) 555-1213
TEL;TYPE=HOME,TYPE=VOICE:(404) 555-1213
EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com
EMAIL;TYPE=INTERNET:example@example.com
EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com
EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com
EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com
EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com
EMAIL;TYPE=INTERNET:example@example.com
EMAIL;TYPE=INTERNET:example@example.com
EMAIL;TYPE=INTERNET:example@example.com
EMAIL;TYPE=INTERNET:example@example.com
EMAIL;TYPE=INTERNET:example@example.com
EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com
EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com
EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com
EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com
EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com
ADR;TYPE=HOME:;;42 Plantation St.;Baytown;LA;30314;United States of America
URL:https://www.google.com/
PHOTO;VALUE=URL;TYPE=PNG:http://upload.wikimedia.org/wikipedia/commons/thumb/a/a5/Example_svg.svg/200px-Example_svg.svg.png
AGENT:BEGIN:VCARD
 VERSION:3.0
 N:Doe;John;Q.,Public
 FN:John Doe
 TEL;TYPE=WORK,VOICE:(111) 555-1212
 TEL;TYPE=HOME,VOICE:(404) 555-1212
 TEL;TYPE=HOME,TYPE=VOICE:(404) 555-1213
 EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com
 EMAIL;TYPE=INTERNET:example@example.com
 PHOTO;VALUE=URL;TYPE=PNG:http://upload.wikimedia.org/wikipedia/commons/thumb/a/a5/Example_svg.svg/200px-Example_svg.svg.png
 END:VCARD
END:VCARD

我使用了StackOverflow中的以下解决方案,但它没有解决问题,因为并非所有重复都连续出现。

perl -ne 'print unless (defined($prev) && ($_ eq $prev)); $prev=$_'

导致:

...
TEL;TYPE=WORK,VOICE:(111) 555-1212
TEL;TYPE=HOME,TYPE=VOICE:(404) 555-1213
TEL;TYPE=WORK,VOICE:(111) 555-1212
TEL;TYPE=HOME,TYPE=VOICE:(404) 555-1213
TEL;TYPE=WORK,VOICE:(111) 555-1212
TEL;TYPE=HOME,TYPE=VOICE:(404) 555-1213
EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com
EMAIL;TYPE=INTERNET:example@example.com
EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com
EMAIL;TYPE=INTERNET:example@example.com
EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com
EMAIL;TYPE=INTERNET:example@example.com

1 个答案:

答案 0 :(得分:3)

删除所有重复行的最简单方法是

perl -ne 'print if !$seen{$_}++'

如果您想分别处理每个BEGIN:VCARD部分,

perl -ne '%seen = () if /\bBEGIN:VCARD\b/; print if !$seen{$_}++'