0

I have hundreds of .xml files in a single directory, and I want to automatically add the same three lines of code to every file.

In each file, after the first instance of this line:

            </staff-details>

I want to add these lines:

            <transpose>
                <chromatic>-2</chromatic>
            </transpose>

The string </staff-details> appears multiple times in each file, but I only want to insert the three lines after the first time the string appears.

Please could someone tell me how to do this with a Windows utility (command-line is fine)?

I've found various similar questions here, but I haven't managed to make any of the answers work for this situation (which is no doubt because I am a novice at this kind of thing).

Thanks!

1 Answers1

0

I finally figured out a way to do this using Sed, by adapting the code from this post: How to use awk to insert multiple lines after first match of a pattern, in multiple files

I saved the lines I wanted to add to a file named insert – saved in the same directory as the XML files – and then ran the following Sed command in that directory:

sed -i -e "0,/<\/staff-details>/{/<\/staff-details>/r insert" -e "}" *.xml