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>

Francis Shi
  • 395
  • 4
  • 8
  • 1
    Possible duplicate of [Replace dynamic content in XML file](http://stackoverflow.com/questions/13369933/replace-dynamic-content-in-xml-file) – Sergey Eremin Mar 08 '16 at 09: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. – Tom Fenech Mar 08 '16 at 09:21
  • the items may be so many,so it should be select by "app_name",then change the value. – Francis Shi Mar 08 '16 at 09:28
  • Please [edit] your question to show us a more complete example along with the desired output. – Tom Fenech Mar 08 '16 at 09:42
  • I update the question : ) Hope it can be understood – Francis Shi Mar 08 '16 at 10:38

3 Answers3

1

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

open file.xml ;
insert text " 2" into /resources/string ;
save :b ;
choroba
  • 231,213
  • 25
  • 204
  • 289
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

SSSDDD
  • 1
  • 1
  • -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)" – Francis Shi Mar 09 '16 at 03:44
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
methuselah-0
  • 96
  • 1
  • 5