0

how can I change the value in xml using bash

<resources> <string name="app_name">Keep Accounts</string> <string name="login">"login"</string> <string name="login_password">"password:"</string> <string name="login_account_hint">input to login</string> <string name="login_password_hint">input your password</string> <string name="login_fail">login failed</string> </resources>

For example I want to change the value "Keep Accounts" to "Keep Accounts 2".The "app_name" may not only be "Keep Accounts",but also other values,let's say XXX,but the "app_name" is certain,I want the output be:

<resources> <string name="app_name">XXX 2</string> <string name="login">"login"</string> <string name="login_password">"password:"</string> <string name="login_account_hint">input to login</string> <string name="login_password_hint">input your password</string> <string name="login_fail">login failed</string> </resources>

5
  • 1
    Possible duplicate of Replace dynamic content in XML file Commented Mar 8, 2016 at 9:20
  • I suspect that you need to show us a bit more context; at the moment your problem can be solved by sed 's/Keep Accounts/& 2/' file.xml but it's the wrong tool for the job and probably won't work with your real data. Commented Mar 8, 2016 at 9:21
  • the items may be so many,so it should be select by "app_name",then change the value. Commented Mar 8, 2016 at 9:28
  • Please edit your question to show us a more complete example along with the desired output. Commented Mar 8, 2016 at 9:42
  • I update the question : ) Hope it can be understood Commented Mar 8, 2016 at 10:38

3 Answers 3

1

Use a XML aware tool. I'd do it in xsh:

open file.xml ;
insert text " 2" into /resources/string ;
save :b ;
Sign up to request clarification or add additional context in comments.

1 Comment

Looks like a nice tool!
0
sed -i 's/<string name=\"app_name\">.*<\/string>/<string name=\"app_name\">NewName<\/string>/g' xml_filename.xml

The single line sed (bash) solution to replace any 'app_name'

Use sed -i to replace the contents of the file in question

I assumed you have the xml content in a file named xml_filename.xml but change this as required

1 Comment

-i.bu is Ok. But I want the new name based on the old name. For example the old name is "Hello",the new name should be "Hello(new)"
0

if you have python-pip installed, you can install xq with pip install yq, then try this(I have tested it works):

xmldoc=$(cat <<'EOF'
<resources>
    <string name="app_name">Keep Accounts</string>
    <string name="login">"login"</string>
    <string name="login_password">"password:"</string>
    <string name="login_account_hint">input to login</string>
    <string name="login_password_hint">input your password</string>
    <string name="login_fail">login failed</string>
</resources>
EOF
)
echo "$xmldoc" | xq '.resources.string = ([.resources.string[]|select(."#text" == "Keep Accounts") ."#text" = "Keep Accounts 2"])' -x

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.