You can do this with just one :substitute
, using \(...\)
capture groups, and re-arranging those submatches in the replacement part. It's only complex because there are several groups to juggle around, and because the pattern spans two lines.
:%s/^\(\w\+\)_\(\d\+\)\(\s*:\s*.*\)\n\(\w\+\)_\2\(\s*:\s*.*\),$/\2 : {\r \1\3\r \4\5\r},/
This matches two subsequent lines (note the \n
in the pattern). By using the number (second capture group) in the second line, it ensures to get NAME_1
and the corresponding VALUE_2
.
Tip: This is easier to build as a search /
first, then just reuse the pattern via :%s//...
In the replacement, the newlines are created via the \r
. You'll find a more detailed explanation in the help under :help :substitute
.