0

I have a log file where each entry is delimited by a series of ---, similar to this:

-----------------
Name=Operation1
Time: 100
Status=Success
-----------------
Name=Operation1
Time: 500
Status=Failure
-----------------
Name=Operation2
Time: 200
Status=Success
-----------------
Name=Operation2
Time: 800
Status=Failure
-----------------

I would like to extract all of the entries matching certain criteria (i.e. all entries where Name=Operation1 or all entries where Status=Failure). I thought I could use agrep as follows: agrep -d "---*" -e "Name=Operation1" mylogfile > myfilteredlogfile but after inspecting the file it doesn't seem to filter as I expected. What's wrong with my filtering and how can I extract my desired entries?

don_crissti
  • 82,805
chemdog95
  • 101

2 Answers2

0

If your awk allows for multibyte record separators, try

awk -vRS="-----------------" -vORS="-----------------" '/Failure/' file

Name=Operation1
Time: 500
Status=Failure
-----------------
Name=Operation2
Time: 800
Status=Failure
-----------------
RudiC
  • 8,969
0

How about some perl:

perl -0777 -snE 'say join "---", grep {/$patt/i} split /^-+$/m' -- -patt="status=fail" file
Name=Operation1
Time: 500
Status=Failure
---
Name=Operation2
Time: 800
Status=Failure

That slurps the file in as a single string, splits it into records, finds the matches, and then joins it with an approximation of the record separator.

glenn jackman
  • 85,964