Vim: How to join multiples lines based on a pattern?
Posted
by
ryz
on Super User
See other posts from Super User
or by ryz
Published on 2011-01-30T21:11:18Z
Indexed on
2011/01/30
23:28 UTC
Read the original article
Hit count: 160
I want to join multiple lines in a file based on a pattern that both lines share.
This is my example:
{101}{}{Apples}
{102}{}{Eggs}
{103}{}{Beans}
{104}...
...
{1101}{}{This is a fruit.}
{1102}{}{These things are oval.}
{1103}{}{You have to roast them.}
{1104}...
...
I want to join the lines {101}{}{Apples}
and {1101}{}{This is a fruit.}
to one line {101}{}{Apples}{1101}{}{This is a fruit.}
for further processing.
Same goes for the other lines.
As you can see, both lines share the number 101, but I have no idea how to pull this off. Any Ideas?
/EDIT:
I found a "workaround":
First, delete all preceding "{1" characters from group two in VISUAL BLOCK mode with C-V
(or similar shortcut), then sort all lines by number with :%sort n
, then join every second line with :let @q = "Jj"
followed by 500@q
.
This works, but leaves me with {101}{}{Apples} 101}{}{This is a fruit.}
. I would then need to add the missing characters "{1" in each line, not quite what I want. Any help appreciated.
© Super User or respective owner