Problem: Some XML files need to be edited recently, so the requirement is that there is a statement in Test.xml: name = "ABC", which needs to be replaced with name= ("abc").
After searching for some methods on the Internet, we decided to use the SED tool to implement the following methods:
1. Install Windows version sed:
Go to sourceforge download Sed.exe and its dependent libraries Libiconv2.dll, Libintl3.dll, Regex2.dll in the same directory
2, edit the SED script xml.sed, the content is as follows:
s/name[]*=[]*/name=_ (/G S/\ ("[^"]*\)"/\1")/g
3. Then use the command line:
sed -f xml2txt. sed test. XML >tmpc\test.txt
To replace the contents of the Test.xml and output the results to test.txt. The command line can be written to a batch, processing multiple XML files at one time.
Q: Why not just write a line of sed s///test.xml and write an sed script alone?
A: 1, if the SED command has special characters such as quotation marks or spaces, the whole command should be enclosed in quotation marks, and the special characters inside the command should be escaped, for example: sed "s/\"//"test.xml;
2. In order to avoid the greedy matching of SED, we need to use [^ "] to match the inner content of quotation marks.
For the above two reasons, if you use the SED command directly on the command line, you need to write this: sed "s/name[]*=[]*\ (\" [^\ "]\" \)/name= (\1) "Test.xml.
Then the question came, how to get sed to correctly interpret [^\] for the results we want (anyway I tried n times not t-t)?
I do not know so the use of the above method of invoking the script, but also the master for me to answer.
sed for Windows double quotes internal content substitution