0

I am trying to change XML files using a Perl one-liner. The XML file is set up as

<NameBool name="Hardware Enabled">
    <BoolConfig>
       <value>false

I need to change the file to true. I have written a one-liner that seems to execute, but it never actually changes the file. What am I doing wrong?

Code

perl -p0777i -e 's/<NamedBool name=\"HardwareEnabled\">\n<BoolConfig>\n<value>false/<NamedBool name=\"HardwareEnabled\">\n<BoolConfig>\n<value>true/m' filename*
1
  • 2
    Don't you need to look for the spaces between the XML tags in your regex? Commented May 2, 2014 at 15:06

2 Answers 2

2

Why do you need a one-liner? Is it really so complicated to write a proper (small) script that would be easier to debug and maintain?

Anyway, provided you lines are long enough ;--)

perl -MXML::Twig -e'XML::Twig->new( twig_handlers => { q{NameBool[@name="Hardware Enabled"]/BoolConfig/value} => sub { $_->set_text( "false")->flush; } }, keep_spaces => 1)->parsefile_inplace( "to_change.xml")'
Sign up to request clarification or add additional context in comments.

2 Comments

I am needing it to be one line because I am actually using it as a call in a python script.
why don't you use a Python library to process the XML then?
0

As was said in the comments above, you are not taking into account that there is indentation in your files. If there is not much else in there, how about this very simple regex:

s/\bfalse\b/true/

If however there are other occurrences of false, you will have to stick to your regex, but add whitespace. You could keep indentation and such, by doing it like this:

s/(<NameBool name="Hardware Enabled">\s+<BoolConfig>\s+<value>)false/${1}true/m

Note that there were two differences between your example data and your regex:

  • example: NameBool, regex: NamedBool
  • example: "Hardware Enabled", regex: "HardwareEnabled"

Take a look at http://regex101.com/r/wZ8bN5.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.