0

I'm trying to figure out a way to extract specific sections from a log file. Each part is split by a fixed string "<-------------->".

<-------------->
abc: def1
ghi: jkl1
xyz generated: def1
<-------------->
abc: def2
ghi: jkl2
xyz generated: def2
Dec 04, 2021 2:51:05 AM abcdefg
SEVERE: [Line 447.149] Lorem ipsum dolor sit amet at [Line 212.2]
Dec 04, 2021 2:51:05 AM abcdefg
SEVERE: [Line 1315.78] Lorem ipsum dolor sit amet at [Line 147.138]
xyz generated: def2
<-------------->
abc: def3
ghi: jkl3
xyz generated: def3
<-------------->

Could sed/grep be used to extract those sections containing the word SEVERE or WARNING within the section?

An alternative approach could be to count line numbers. Sections having only 3 lines can be dropped. All other sections having more lines are the ones I'm looking to extract.

1 Answer 1

4

You can treat each block as a record in GNU AWK:

awk -v RS='<-------------->\n' -v ORS='' '/SEVERE|WARNING/'

This treats the separator, and its trailing new line, as the record separator. Any record matching “SEVERE” or “WARNING” is output, with no separating new line; if you wish to keep matching records separated, you can leave ORS set to its default value:

awk -v RS='<-------------->\n' '/SEVERE|WARNING/'

This will add a new line after every matching record, including the last one.

1
  • That was too easy. Thx! 🙂
    – PieterB
    Dec 4, 2021 at 16:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .