I have a XML file(MyXML.xml) like this :

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
        <ns3:GetAllInfoFromRest xmlns:ns2="http://com.lanuk.cfe/b2_7/service/objects" xmlns:ns3="http://com.lanuk.cfe/b2_7/service/operations">
            1111,GH43567,Hamburger,GET,278598655,\n000001,                    ,Kunal,Bhyuo,Ramond,856 K. 98 Rd,                                        ,                                        ,Tripura,AGT,INDIA,856987,                    ,S,S,S,8956,\666666

Now i need to strip out the SOAP content and all the tag attributes from this xml and get only the string response 1111,GH43567,Hamburger,GET,278598655,\n000001, ,Kunal,Bhyuo,Ramond,856 K. 98 Rd, , ,Tripura,AGT,INDIA,856987, ,S,S,S,8956,\666666.

How can i do it with awk or sed ?

I tried it in this way :

$ xgawk -lxml 'XMLATTR["xmlns:ns3"]=="http://com.lanuk.cfe/b2_7/service/operations"{print $2}' MyXML.xml

But obviously I am making some mistake due to which it is not working. Can some one suggest any other way around this ?

marked as duplicate by slm, Anthon, rahmu, terdon, manatwork Sep 19 '13 at 15:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


sed/awk are really about regular expressions. check this answer on stackoverflow why parsing HTML/XML with regular expressions is a bad idea.

for XML you really need to build a DOM of the document and then find your information. there are cmdline tools like xmlstar that allow you to get information out of XML-documents.

but do not try using sed/awk to parse XML

PS: of course, you might be able to create a simple regular expression that can extract the information needed on the files you happen to encounter in real life. e.g. the following will print the 5th line of the document, which (in your example) holds the relevant information.

# stupid and naive approach:
sed '5!d' MyXML.xml

but this makes an assumption about the layout of the file, which has nothing to do with XML. it might work for a very specific generator of the given file, but is not guaranteed to work with any XML-file following the same structure (and structured data is what XML is all about)

  • There are also XML parsers in most scripting languages - Perl, Python, Ruby... – peterph Sep 19 '13 at 13:15
  • @TheDarkKnight - Though this answers your question, it's really not a good idea to be parsing structured data such as JSON or XML using sed and awk. Use a real parser specifically suited to the data! – slm Sep 19 '13 at 17:25
  • @slm: Can you explain like what you mean by real parser? – The Dark Knight Sep 19 '13 at 17:29
  • @TheDarkKnight - sure. A tool such as xmlstar.sourceforge.net. Also in Perl there are modules for parsing JSON or XML which give you methods for querying/manipulating the XML/JSON. – slm Sep 19 '13 at 17:48
  • @slm my answer is all about using tools like xmlstar (2nd paragraph) rather than trying to parse XML using sed&awk (first paragraph). i tried to make clear that my trivial sed-script does not parse XML at all – umläute Sep 19 '13 at 21:00

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