简体   繁体   中英

How to add multiple lines of code to multiple xml files using a Windows-based solution

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM