Reputation: 91
Hi I got segment of log and I need to extract parts between the tags and tags must be included.
The log looks like this
2016/10/28 13:22:17 INFO
2016/10/28 13:22:17 INFO
<Command
tId="27810237892289-789766789"
user="root">
<ChangeTAG
a_var="22341431413"
b_var="837431243413"
status="activated"></ChangeTAG>
</Command> application=ui_6789 state=5 state_current=na
2016/10/28 13:22:19 INFO
2016/10/28 13:22:19 INFO
<Command
tId="27810567892289-701226789"
user="root">
<ChangeTAG
a_var="87656651413"
b_var="345751243413"
status="activated"></ChangeTAG>
</Command> application=ui_3444 state=1 state_current=na
2016/10/28 13:22:29 INFO
2016/10/28 13:22:29 INFO
and output should be like:
<Command
tId="27810237892289-789766789"
user="root">
<ChangeTAG
a_var="22341431413"
b_var="837431243413"
status="activated"></ChangeTAG>
</Command>
<Command
tId="27810567892289-701226789"
user="root">
<ChangeTAG
a_var="87656651413"
b_var="345751243413"
status="activated"></ChangeTAG>
</Command>
any Idea how to chop it from log in sed awk or grep ?
Upvotes: 0
Views: 88
Reputation: 203502
With GNU awk for multi-char RS, RT, and gensub():
$ awk -v RS='</Command>' 'RT{print gensub(/.*(<Command)/,"\\1",1) RT}' file
<Command
tId="27810237892289-789766789"
user="root">
<ChangeTAG
a_var="22341431413"
b_var="837431243413"
status="activated"></ChangeTAG>
</Command>
<Command
tId="27810567892289-701226789"
user="root">
<ChangeTAG
a_var="87656651413"
b_var="345751243413"
status="activated"></ChangeTAG>
</Command>
Upvotes: 0
Reputation: 67497
awk
to the rescue!
$ awk '/<Command/{p=1} /<\/Command/{printf "%s", $1; p=0} p' file
<Command
tId="27810237892289-789766789"
user="root">
<ChangeTAG
a_var="22341431413"
b_var="837431243413"
status="activated"></ChangeTAG>
</Command> <Command
tId="27810567892289-701226789"
user="root">
<ChangeTAG
a_var="87656651413"
b_var="345751243413"
status="activated"></ChangeTAG>
Upvotes: 0