Search code examples
xmlbashunixsedcygwin

Parsing log file with sed in linux


I'm trying to use sed for parsing log file to extract xml messages from it using the template. I want to get all xml messages in new file.

I'm using this command sed 's/<sending>\(.*\)<\/sending>/\1/' input.out>output.xml

input.out have following сontent:

 WARNING: Exchange[ExchangePattern: InOut, BodyType: byte[], Body: <?xml version="1.0" encoding="utf-8" standalone="yes"?><sending><query>        <describe>            <data>city</data>        </describe>    </query></sending>]
Sep 26, 2016 11:54:30 AM org.apache.camel.util.CamelLogger log
WARNING: Exchange[ExchangePattern: InOut, BodyType: byte[], Body: <?xml version="1.0" encoding="utf-8" standalone="yes"?><sending>    <query>        <key_info/>    </query></sending>]

I'm expecting to get result like this:

 <query>        <describe>            <data>city</data>        </describe>    </query>    <query>        <key_info/>    </query>

But i only get source file without <sending> and </sending> elements like this:

WARNING: Exchange[ExchangePattern: InOut, BodyType: byte[], Body: <?xml version="1.0" encoding="utf-8" standalone="yes"?><query>        <describe>            <data>city</data>        </describe>    </query>]
Sep 26, 2016 11:54:30 AM org.apache.camel.util.CamelLogger log
WARNING: Exchange[ExchangePattern: InOut, BodyType: byte[], Body: <?xml version="1.0" encoding="utf-8" standalone="yes"?>    <query>        <key_info/>    </query>]

Sorry for my english and have a nice day. Thank you for help.


Solution

  • sed 's/.*<sending>\(.*\)<\/sending>.*/\1/' input.out>output.xml
    

    you forget to remove surrounding part of the line (.*), sed only take the content of your pattern and you don't include char before or after your sending bloc

    now i suggest

    sed -e '/.*<sending>\(.*\)<\/sending>.*/!d' -e 's//\1/' input.out>output.xml
    

    it remove line that don't have the sending bloc. Don't forget that sed take the longest partterne available so if there are 2 bloc on same line (or more) it take from first sending until last /sending