1

I'm trying to parse some data from a log file. The premises are that i need a date that is 2 lines above the the line with the pattern that i want.

I'm able to reach this with grep:

> grep -B2 "rc_transaction result=" xml.log

And the output is 3 lines for each pattern find:

03 Apr 12:48:42.503 [6086-16592] DEBUG c.q.q.d.server.XmlServlet - <?xml version="1.0"?>
<stream id="18561">
<rc_transaction result="ok" vst_time="2018-04-03 10:48:42.431" transaction_time="2018-04-03 10:48:42.497" sequence_number="117749" code="0">
--
03 Apr 12:49:21.936 [6086-16592] DEBUG c.q.q.d.server.XmlServlet - <?xml version="1.0"?>
<stream id="18566">
<rc_transaction result="ok" vst_time="2018-04-03 10:49:21.839" transaction_time="2018-04-03 10:49:21.930" sequence_number="117750" code="0">
--
03 Apr 12:49:39.654 [6086-16592] DEBUG c.q.q.d.server.XmlServlet - <?xml version="1.0"?>
<stream id="18569">
<rc_transaction result="ok" vst_time="2018-04-03 10:49:39.582" transaction_time="2018-04-03 10:49:39.648" sequence_number="117751" code="0">

Now, i need to parse this results an get the date on the first line and some properties from the third line, something like this:

03 Apr 12:48:42.503 result="ok" sequence_number="117749"
03 Apr 12:49:21.936 result="ok" sequence_number="117750"
03 Apr 12:49:39.654 result="ok" sequence_number="117751"

Whats the best method to achieve this?

3
  • It's not either of the timestamps in the actual XML that you are interested in then? Commented Apr 7, 2018 at 18:43
  • No. I need the time of when the transaction was processed (logged) and some data about it Commented Apr 7, 2018 at 18:47
  • 1
    @Kusalananda I understand now what you asked. The dates are different altough in the example they seem similar. But they can deffer on some minutes sometimes. Commented Apr 7, 2018 at 18:49

1 Answer 1

1

Awk solution:

awk '/<\?xml/{ date=$1 OFS $2 OFS $3 }/rc_transaction result=/{ print date, $2, $7 }' xml.log

The output:

03 Apr 12:48:42.503 result="ok" sequence_number="117749"
03 Apr 12:49:21.936 result="ok" sequence_number="117750"
03 Apr 12:49:39.654 result="ok" sequence_number="117751"
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.