2

I want to copy strings that spans across multiple lines between two markers in a large file.

In the below example file, start marker is <ROW> and end marker is </ROW>.

<ROW>
    <SOURCE>ANIMAL</SOURCE>
    <QTY>1.00</QTY>
    <UNIT_PRICE>123.75</UNIT_PRICE>
    <NARRATIVE>DOG100</NARRATIVE>
    <OUR_REFERENCE>BILLING</OUR_REFERENCE>
</ROW>
<ROW>
    <SOURCE>ANIMAL</SOURCE>
    <QTY>1.00</QTY>
    <UNIT_PRICE>150.00</UNIT_PRICE>
    <NARRATIVE>DOG200</NARRATIVE>
    <OUR_REFERENCE>BILLING</OUR_REFERENCE>
</ROW>
<ROW>
    <SOURCE>ANIMAL</SOURCE>
    <QTY>1.00</QTY>
    <UNIT_PRICE>170.00</UNIT_PRICE>
    <NARRATIVE>CAT300</NARRATIVE>
    <OUR_REFERENCE>BILLING</OUR_REFERENCE>
</ROW>
<ROW>
    <SOURCE>BIRD</SOURCE>
    <QTY>1.00</QTY>
    <UNIT_PRICE>200.00</UNIT_PRICE>
    <NARRATIVE>PARROT300</NARRATIVE>
    <OUR_REFERENCE>BILLING</OUR_REFERENCE>
</ROW>
.
.
.

I want to search and copy the string between <ROW> and </ROW> (including) that matches the text DOG

Example output

<ROW>
    <SOURCE>ANIMAL</SOURCE>
    <QTY>1.00</QTY>
    <UNIT_PRICE>123.75</UNIT_PRICE>
    <NARRATIVE>DOG100</NARRATIVE>
    <OUR_REFERENCE>BILLING</OUR_REFERENCE>
</ROW>
<ROW>
    <SOURCE>ANIMAL</SOURCE>
    <QTY>1.00</QTY>
    <UNIT_PRICE>150.00</UNIT_PRICE>
    <NARRATIVE>DOG200</NARRATIVE>
    <OUR_REFERENCE>BILLING</OUR_REFERENCE>
</ROW>

How can I do this in gvim?

3

If you have indent-object, you could do something like

:global/DOG/normal "Ayii

I haven’t come up with a simple vanilla answer yet, but maybe

:global/DOG/ ?ROW?,/ROW/yank A

@Lie Ryan reminded me of the at text-object, giving

:global/DOG/normal "Ay2at

Otherwise I’d use a dedicated XML parser and copy the output.

| improve this answer | |
2

You can use the HTML/XML tag text object:

:g/DOG/norm v2at"AY

Or if you have a way to reformat the XML afterwards, then this shorter command also works:

:g/DOG/norm y2at

Both should work in standard vim without plugins.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.