Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I print from a specific string to a new line with a matching pattern

Status
Not open for further replies.

johngiggs

Technical User
Oct 30, 2002
492
0
0
US
Hi,

I have a log file I want to parse. Lines normally begin like this:

2022-04-07 12:46:14,624-0400 some text here
2022-04-07 12:46:14,625-0400 some more text here
2022-04-07 12:46:14,625-0400 ERROR something
oh my gosh
this is terrible
bad error message
2022-04-07 12:46:14,625-0400 some more text here
2022-04-07 12:46:14,625-0400 some more more text here


I want to capture from the beginning of an error line until the next new line with the timestamp.

What I tried to do is this:

awk '/ERROR something/,/^2022/' mylog.txt

But this didn't work because the pattern match works on my first line. I want to find the next line with the string ^2022, so basically print the current line until the next line of ^2022.

The desired output would be:

2022-04-07 12:46:14,625-0400 ERROR something
oh my gosh
this is terrible
bad error message

Thanks in advance!
 
Hi,
you can try something like this

johngiggs.awk
Code:
{
  if ($0 ~ /[0-9]{4}-[0-9]{2}-[0-9]{2}/) {
    previous_line = $0
    not_printed = 1 
  } 
  else {
    if (not_printed) {
      print previous_line
      not_printed = 0      
    }
    print
  }
}

On the data file you provided

johngiggs.txt
Code:
2022-04-07 12:46:14,624-0400 some text here
2022-04-07 12:46:14,625-0400 some more text here
2022-04-07 12:46:14,625-0400 ERROR something
oh my gosh
this is terrible
bad error message
2022-04-07 12:46:14,625-0400 some more text here
2022-04-07 12:46:14,625-0400 some more more text here

the output of the script above is:
Code:
$ awk -f johngiggs.awk johngiggs.txt
2022-04-07 12:46:14,625-0400 ERROR something
oh my gosh
this is terrible
bad error message
 
Hi,

Thank you for that suggestion. It did work for the example, however my example wasn't clear enough. There are some WARN lines that I want to ignore. I want to capture ERROR and the subsequent lines below it until the next new line with a datestamp.

2022-04-07 12:46:14,624-0400 some text here
2022-04-07 12:46:14,625-0400 some more text here
2022-04-07 12:46:14,625-0400 ERROR something
oh my gosh
this is terrible
bad error message
2022-04-07 12:46:14,625-0400 some more text here
2022-04-07 12:46:14,625-0400 some more more text here
2022-04-07 12:46:14,625-0400 WARN something else goes here
2022-04-07 12:46:14,625-0400 ERROR something
oh my gosh
this is terrible
bad error message
2022-04-07 12:46:14,625-0400 some more text here
2022-04-07 12:46:14,625-0400 some more more text here

Thanks!
 
Hi,
it works too with the another input file:

Code:
$ gawk -f johngiggs.awk johngiggs.txt
2022-04-07 12:46:14,625-0400 ERROR something
oh my gosh
this is terrible
bad error message
2022-04-07 12:46:14,625-0400 ERROR something
oh my gosh
this is terrible
bad error message
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top