Page 1 of 1

Find/Replace in Files for Multiple Lines in an XML file

Posted: Wed May 04, 2022 3:46 am
by mkemezis
Hi all,
I am working on a metadata clean up project. I need to make the same specific changes to dozens and sometimes hundreds of record in batches. I am having issues using the Find/Replace in files function when I have to make changes to the structure of the metadata. The single file find/replace works well in that I can locate an entire structured metadata element and modify or delete. When I use the Fine/Replace in files function, it will only let me find and replace the first line of text from the XML document.
I want to delete the data in the element where null is the value. In the example below I want to remove all of the following lines from a dozen records:

Code: Select all

<mods:name type="corporate" authority="local">
      <mods:namePart>null</mods:namePart>
      <mods:role>
         <mods:roleTerm type="text" authority="marcrelator">null</mods:roleTerm>
      </mods:role>
   </mods:name>
But when I do Search/Replace in flies, the Text to find text box will only let me include the first line <mods:name type="corporate" authority="local"> as the text to search for,
I cannot seem to figure out how to searching across multiple lines across multiple files. Any help would be appreciated.
Thanks,
Mike

Re: Find/Replace in Files for Multiple Lines in an XML file

Posted: Wed May 04, 2022 12:21 pm
by adrian
Hi,

You can use \n in the "Text to find" box to indicate a new line or \s for any whitespace (line breaks, tabs and spaces). The "Regular expression" box is required in this case.
So, if you really want to do this via Find/Replace in Files you should search for:

Code: Select all

^\s*\Qline1\E\s*\Qline2\E .... \s*\QlineN\E
\Qtext\E means the searched text is "literal" (not a regular expression).
e.g.

Code: Select all

^\s*\Q<mods:name type="corporate" authority="local">\E\s*\Q<mods:namePart>null</mods:namePart>\E\s*\Q<mods:role>\E\s*\Q<mods:roleTerm type="text" authority="marcrelator">null</mods:roleTerm>\E\s*\Q</mods:role>\E\s*\Q</mods:name>\E
Note that Find/Replace in Files may not be the tool best suited for this job. But I guess it can get the job done if used right.

Regards,
Adrian