0

I have a log file and I need to extract the sections related to a specific event. I performed something similar before but easier with the "sed" command.

The task I've been trying to solve is the following. I need to extract all the portions of text in the file starting with the word "BEGIN" included and ending before the next "BEGIN" where it is included a specific word identifying a table in my database. I need help with the usage of the sed command.

The task can be summarized in "search between all occurrences delimited by Start and End containing the word word"

The text looks like something like this:

BEGIN
...
something here
something here containing the desired "word"
something here
...
COMMIT
BEGIN 
.
.
.
COMMIT
BEGIN

And I want to obtain all the occurrencies conaining "word" as this:

BEGIN
...
something here containing the desired "word"
...
COMMIT
  • Is every BEGIN END section in its own line, or are the multiple of them inside one line – ralz Dec 28 '20 at 15:24
  • Does this answer your question? printing a block of text that matches a pattern - This answer uses awk to a) select text blocks between START and END keywords and b) only prints the block if the MATCH keyword is found. – FelixJN Dec 28 '20 at 15:35
  • Please also show the expected output you want to get. Please confirm if the BEGIN and COMMIT lines will always be separate lines as shown in the example. Can there be more than one line between BEGIN and COMMIT? Please [edit] your question to add requested information, don't use comments to answer. Additional requirements should also be added to the question. – Bodo Dec 28 '20 at 15:37
  • maybe awk 'BEGIN{RS="BEGIN"} /word/' inputfile or awk 'BEGIN{RS="(\n*COMMIT)?\n*BEGIN\n*"} /word/' inputfile or awk 'BEGIN{RS="(COMMIT\n*)?BEGIN\n*"} /word/' inputfile? – Bodo Dec 28 '20 at 15:50
  • @Fiximan looks like it does. I'll try. Thank you so much! – Davide Carta Dec 28 '20 at 16:08

0 Answers0