sed for Windows double quotes internal content substitution

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.