Reputation: 83
How can I replace duplicate strings of words in each row using regular expressions with vi? For instance, I have the following list of seaports that have duplicate text.
My goal is to achieve the following output below:
(input) Duplicated List
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
(output) Corrected List
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)
(...)
Any insight?
(full list of ports available in http://www.photius.com/seaports/)
Upvotes: 1
Views: 120
Reputation: 36272
Apply a substitution command for each line of the file:
:%s/\v(.*)\s*\1/\1/
It yields:
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
Upvotes: 5