Wednesday, December 23, 2020

Re: Substitute pattern over multiple lines

On 2020-12-23 17:48, John Cordes wrote:
> I'm seeking help with editing a GEDCOM (genealogy) file. For
> this I'm using Vim 8.2 in Windows. Here is a segment of text from
> the file (the language doesn't make sense since I've deleted
> some internal lines in the NOTEs which aren't relevant to the
> question):
>
> =======================
> 1 EVEN
> 2 TYPE tngnote
> 2 NOTE I have included the children William, Charles, Alice, and
> with his parents in 1881, and with his widowed mother in 1
> 3 CONC 891 (e.g. see my online transcription of the 1891 Smiths
> with James Moser, son of Henry Moser and Mary Henneberry, and his
> wife Margaret Woodin; however
> 3 CONC , I have not yet taken this step.
> 1 BIRT
> =======================
>
> The 2 lines beginning with ^3 CONC are Continuation
> (CONC=Concatenation) lines.
>
> I want to surround the text of the NOTE with a 'div' tag, so that
> the final result should look like this:
>
> =======================
> 1 EVEN
> 2 TYPE tngnote
> 2 NOTE <div class="xxx">I have included the children William,
> Charles, Alice, and with his parents in 1881, and with his widowed
> mother in 1891 (e.g. see my online transcription of the 1891
> Smiths with James Moser, son of Henry Moser and Mary Henneberry,
> and his wife Margaret Woodin; however, I have not yet taken this
> step.</div>
> 1 BIRT
> =======================

I'd start with this ugly monstrosity:

:%s/^2 \u\{3,} \zs\(.*\n\(\%(\D\|3 CONC \).*\n\)\+\)/\='<div
class="xxx">'.substitute(substitute(submatch(1), '\n3 CONC ', '',
'g'), '\n', '', 'g')."<\/div>\n"

(all one line in case it breaks in the mail)

If you only want it to do "2 NOTE" lines, you can change that initial

2 \u\{3,} \zs

(which does any item that has continuations) to

2 NOTE \zs

This does join *all* the lines and doesn't re-wrap them, so you'd
then want a second pass to do the wrapping

:set tw=70
:g/<div [^>]*>.*<\/div>$/norm gqq

Hope this gives you some ideas to work with.

-tim



--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20201223170832.7ca70687%40bigbox.attlocal.net.

No comments: