bash script to edit xml file

前端 未结 4 1970
再見小時候
再見小時候 2021-02-06 00:53

I want to edit the config file of a program that is an XML:


   
       ...
       

        
相关标签:
4条回答
  • 2021-02-06 01:19

    Using xmlstarlet:

    xmlstarlet val -e file.xml
    xmlstarlet ed -u "//settings/setting/@name" -v 'local directory2' file.xml
    xmlstarlet ed -u "//settings[1]/setting/@name" -v 'local directory2' file.xml
    
    # edit file inplace
    xmlstarlet ed -L -u "//settings/setting/@name" -v 'local directory2' file.xml  
    
    0 讨论(0)
  • 2021-02-06 01:23

    Most people would probably use sed to do line editing from a bash script. If you actually care about parsing the XML, then use something like Perl which has a ready XML parser.

    0 讨论(0)
  • 2021-02-06 01:31

    An ugly/unsafe but sometimes the easiest is to call sed/perl/awk from bash

    0 讨论(0)
  • 2021-02-06 01:34

    Depending on what you want to do, you may want to use some XML-specific tooling (to handle character encodings, to maintain XML well-formedness etc.). You can use the normal line-oriented tools, but unless you're careful (or doing something trivial) you can easily create non-compliant XML.

    I use the XMLStarlet command line set. It's a set of command line utilities for specifically parsing/manipulating XML.

    0 讨论(0)
提交回复
热议问题