Pregunta:
Dada una serie de líneas que se parecen a esto:
2001 "Some Kind of Title," Author's Name, Publication Name, 1 Mar.
2002 "Some Kind of Title," Author's Name, Publication Name, 12 Oct.
2003 "Some Kind of Title," Author's Name, Publication Name, 8 Apr.
2004 "Some Kind of Title," Author's Name, Publication Name, 3 Jun.
¿Hay alguna manera de que pueda tomar esos primeros cuatro caracteres (el año) y copiarlos al final de la línea, para que se vea así:
2001 "Some Kind of Title," Author's Name, Publication Name, 1 Mar. 2001
2002 "Some Kind of Title," Author's Name, Publication Name, 12 Oct. 2002
2003 "Some Kind of Title," Author's Name, Publication Name, 8 Apr. 2003
2004 "Some Kind of Title," Author's Name, Publication Name, 3 Jun. 2004
Respuesta:
:% s/\v^(\d{4})(.*)$/\1\2 \1/
es una forma de hacerlo
-
\v
opción mágica, para evitar tener que escapar de la agrupación()
-
^
inicio de línea -
\d{4}
coincide exactamente con cuatro dígitos -
.*
resto de línea -
\1
\2
tiene el patrón coincidente dentro de()
editar: @Jair Lopez menciona en los comentarios, la expresión regular se puede mejorar aún más:
:% s/\v^(\d{4}).*/& \1/
o el equivalente
:% s/\v^(\d{4}).*/\0 \1/
&
y\0
contiene todo el patrón coincidente
Para obtener más información, consulte las preguntas frecuentes sobre vimregex y regex