如何使用vi的正则表达式替换每行中重复的单词串? 例如,我有以下具有重复文本的海港列表。
我的目标是在下面获得以下输出:
(输入)重复列表
Andorra La Vella Andorra La Vella
Al Ain Al Ain
Abu al Bukhoosh Abu al Bukhoosh
Ajman Ajman
Arzanah Island Arzanah Island
Abu Dhabi Abu Dhabi
Das Island Das Island
Al Dhafra Al Dhafra
Ras Zubbaya (Ras Dubayyah) Ras Zubbaya (Ras Dubayyah)
Dubai Dubai
Fateh Terminal Fateh Terminal
Fujairah (Al-Fujairah) Fujairah (Al-Fujairah)
Musafa Musafa
Hamriyah Hamriyah
Jebel Ali Jebel Ali
Jebel Dhanna Jebel Dhanna
Khor Al Fakkan Khor Al Fakkan
Masfut Masfut
Mubarras Island Mubarras Island
(输出)更正列表
Andorra La Vella
Al Ain
Abu al Bukhoosh
Ajman
Arzanah Island
Abu Dhabi
Das Island
Al Dhafra
Ras Zubbaya (Ras Dubayyah)
Dubai
Fateh Terminal
Fujairah (Al-Fujairah)
(...)
有什么见解?
(http://www.photius.com/seaports/中可用的完整端口列表)
答案 0 :(得分:5)
为文件的每一行应用替换命令:
:%s/\v(.*)\s*\1/\1/
它产生:
Andorra La Vella
Al Ain
Abu al Bukhoosh
Ajman
Arzanah Island
Abu Dhabi
Das Island
Al Dhafra
Ras Zubbaya (Ras Dubayyah)
Dubai
Fateh Terminal
Fujairah (Al-Fujairah)
Musafa
Hamriyah
Jebel Ali
Jebel Dhanna
Khor Al Fakkan
Masfut
Mubarras Island