I would like to remove the string currencyId="GBP" from an xml file. Please note there is a single space before the first letter c. I am having trouble parsing the data and removing this string (including the initial space) would make it easier for me. I know I can use sed, but the space at the start of the string and double quotes seems to be tripping me up in my attempts so far.

To clarify here is an sample of the xml.

 <shippingServiceCost currencyId="GBP">2.83</shippingServiceCost>
<currentPrice currencyId="USD">157.5</currentPrice>
<convertedCurrentPrice currencyId="GBP">111.45</convertedCurrentPrice>

If I achieve my objective, the convertedCurrentPrice line would read:


sed -i 's/ currencyId="GBP"//' file.xml - This works for me. Add a g at the end of the sed command if you want to replace all instances.

  • Hi yes, I do want to replace all instances, I added the g as you suggested like so sed -i 's/ currencyId="GBP"//g' and it worked , thanks for that
    – neilH
    Apr 8 '16 at 11:25
  • hello again, I've tried to use your suggestion with a different string and this one is problematic the string I now want to remove is </sellingStatus> (I want to remove every character including the slashes and <>) any ideas?
    – neilH
    Apr 9 '16 at 15:02
  • Hi @bms9nmh , please create a new question instead of asking questions in comments.
    – Munir
    Apr 9 '16 at 17:11
  • @bms9nmh : you can update your question too.
    – Rahul
    Apr 12 '16 at 4:38
  • new question created here: unix.stackexchange.com/questions/275971/…
    – neilH
    Apr 12 '16 at 14:26

As you have not specified whether you want to replace the string globally or not, I am assuming it to replace for specific line, so command would be,

sed -i '6s+ currencyId="GBP"++' filename.xml
It's better to backup the file before editing, so to be on safer side I would use,

sed -i.bak '6s+ currencyId="GBP"++' filename.xml


The xmlstarlet command

xmlstarlet ed -d '//convertedCurrentPrice/@currencyId[. = "GBP"]' file.xml

would delete all currencyId attributes from all convertedCurrentPrice nodes in the whole document, given that the attribute's value was GBP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.