在sed或awk

时间:2016-10-28 20:19:13

标签: bash awk sed tags extract

嗨,我得到了一段日志,我需要提取 必须包含标签和标签之间的部分。

日志看起来像这样 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

输出应该是:
<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> 任何想法如何从登录sed awk或grep中砍掉它?

3 个答案:

答案 0 :(得分:1)

使用GNU sed:

sed -n '/<Command/,/<\/Command/p' file

答案 1 :(得分:0)

awk救援!

$ 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>

答案 2 :(得分:0)

使用GNU awk进行多字符RS,RT和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>