0

I need help to filter a part of text from my original logs:

    <variable>  
  <status type="String"><![CDATA[-1]]></status>
  <errorCode type="String"><![CDATA[[bpm]]]></errorCode>
  <mensagens type="MensagemSistema[]">
    <item>
      <msg_err type="String"><![CDATA[ERROR1-This is error: - THIS TEXT IS VARIABLE.]]</msg_err>
      <msg_err_stack type="String"><![CDATA[stack_trace]]></msg_err_stack>
    </item>
  </mensagens>
</variable>  

The part that I want is:

<msg_err type="String"><![CDATA[ERROR1-This is error: - THIS TEXT IS VARIABLE.]]>

... and this text is variable.

I tried to perform this with sed, but I wouldn't find a example to remove text outside two strings. Just another thing this is unix

thanks in advance Tiago

tiago
  • 39
  • 10

2 Answers2

0

You could try the below sed command,

$ echo '<msg_err type="String"><![CDATA[ERROR1-This is error 1.]]></msg_err>' | sed 's/.*\[\([^][]*\).*/\1/g'
ERROR1-This is error 1.
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • sorry, I forgotten to refer something, I'm going edit the original message – tiago Aug 05 '14 at 13:28
  • I tried others things, but at the end I used your approach with grep. This version of AIX is very limited – tiago Aug 05 '14 at 22:25
  • You don't need the grep. Add -n as an argument and a p at the end of the pattern thus: sed -n 's/.*\[\([^][]*\).*/\1/pg' --- Also, do you want the g at the end? That means replace multiple times but I think you will hit only once because of the .* at the end. Sooo... make it: sed -n 's/.*\[\([^][]*\).*/\1/p' – pedz Aug 06 '14 at 00:57
0

This looks like a job for an XML parser. The Perl module XML::Simple is capable of retrieving the data you want:

perl -MXML::Simple -e '$xml = XMLin(\*STDIN); print $xml->{'mensagens'}->{'item'}->{'msg_err'}->{'content'};' < error.xml

Output:

ERROR1-This is error: - THIS TEXT IS VARIABLE.

Note that I added a > to close the CDATA in the msg_err tag, as I assumed this to be a typo.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • I tried this but, don't work ... This is a result of a stack trace / run time error, saved in table of the system. – tiago Aug 05 '14 at 22:27
  • Without more details it's impossible to say why. Assuming the parsing was successful, you might want to try using `Data::Dumper` to print the `$xml` variable and work out exactly what part you want to extract. If you have some more details, you should edit your question. – Tom Fenech Aug 05 '14 at 23:29