1

I'm trying to make a script that changes a few values then deploys. I have this XML:

<application>
    <NVPairs name="Variables">
        <NameValuePair>
            <name>Parameter/1</name>
            <value>MyOtherValue</value>
        </NameValuePair>
        <NameValuePair>
            <name>Parameter/2</name>
            <value>MyValue</value>
        </NameValuePair>
    </NVPairs>
</application>

Which I am trying to edit with SED. The NVPAIRS and NameValue seems to be messing me around a bit. Can I ask for suggestions how to deal with NameValuePair? I did try XMLStarlet as well, but same problem. Can not seem to deal with NameValuePairs.

5
  • My crystal ball isn't working tonight. What are you trying to do and why isn't it working? Commented Jul 11, 2013 at 11:59
  • LOL sorry I am trying to edit The Parameter/2 (which is not a constant) and Myvalue (which is also not a constant). Commented Jul 11, 2013 at 12:03
  • 1
    It's XML. Use something that deals in XML, not in pattern matching. I'd probably use a Perl one-liner with XML::LibXML Commented Jul 11, 2013 at 12:04
  • The values can change. And this NameValuePair's are messing me around a bit. Commented Jul 11, 2013 at 12:05
  • The other problem is I can not install any of the XML tools like XMLStarlet etc since this runs as a normal use, that does not have root access. So I am forced to use the tools at hand. Commented Jul 11, 2013 at 12:06

1 Answer 1

0

I wouldn't try to use sed to edit XML. Unless there is some constant to match on (that lets you decide if you are on a /1 or /2) it'll be hard (I would say impossible, but some sed guru will chime and and prove me wrong...)

If you can't install stuff, something like an awk or perl script that lets you keep a minimum of state you work better than sed.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried awk using the information I found here: stackoverflow.com/questions/8514985/… but as i said the information is not always the same. I tried an XPATH approach, but this is weird XML (name pairs) first time I have to try and edit it using scripts
Awk can be used much more like a scripting language (variables etc) so you can keep state and treat lines differently depending on the state. You really need to provide a sample of the types of values you expect to deal with - until then we're all just guessing!
The example XML i supplied is the kinda values I have to deal with. There is no constant. So SED/AWK might not work. The XML is the "config" file for a JAVA EAR File.

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.