×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

(OP)
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!

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

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 

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

(OP)
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!

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

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 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close