Print match and line after
How about:
awk '$4=="-" && prev4=="frz" {print prevline; print} {prev4 = $4; prevline=$0}' file
If you have GNU grep and your pattern doesn't occur elsewhere in the data, you can try this :
grep -A1 frz | grep -vB1 frz
Explanation
The first grep
captures all lines where the pattern occurs, plus the next one :
-A NUM, --after-context=NUM Print NUM lines of trailing context after matching lines. Places a line containing a group separator (--) between contiguous groups of matches.
The output of this first command on your sample input is :
2018-04-09T14:15:23.978Z 8 multi frz uuid1 uuid3 - -
2018-04-09T14:29:35.826Z 8 multi frz uuid1 uuid3 uuid2 -
2018-04-09T17:19:01.901Z 8 multi frz uuid1 uuid3 uuid2 -
2018-06-03T22:12:38.688Z 8 multi - uuid1 uuid3 uuid2 -
--
2018-06-28T16:29:01.624Z 10 multi frz uuid1 uuid3 uuid2 -
2018-06-28T17:29:01.624Z 10 multi - uuid1 uuid3 uuid2 -
Then the second command searches for lines not containing the pattern, and prints them with the line before :
-B NUM, --before-context=NUM Print NUM lines of leading context before matching lines. Places a line containing a group separator (--) between contiguous groups of matches.
As noted in the grep man page, the ouput contains group separators (--
) :
2018-04-09T17:19:01.901Z 8 multi frz uuid1 uuid3 uuid2 -
2018-06-03T22:12:38.688Z 8 multi - uuid1 uuid3 uuid2 -
--
2018-06-28T16:29:01.624Z 10 multi frz uuid1 uuid3 uuid2 -
2018-06-28T17:29:01.624Z 10 multi - uuid1 uuid3 uuid2 -
You can add a third grep
to remove them if needed :
grep -A1 frz | grep -vB1 frz | grep -v '^--$'
I'd like to offer a completely impractical GNU grep
approach. Works, but looks nasty.
grep -Pzo "^\S+\s+\S+\s+\S+\s+frz\s+.*\n\S+\s+\S+\s\S+\s+\-\s+.*" input
Example.
$ cat file
2018-04-09T14:15:23.366Z 7 multi - uuid1 uuid2 uuid3 -
2018-04-09T14:15:23.978Z 8 multi frz uuid1 uuid3 - -
2018-04-09T14:29:35.826Z 8 multi frz uuid1 uuid3 uuid2 -
2018-04-09T17:19:01.901Z 8 multi frz uuid1 uuid3 uuid2 -
2018-06-03T22:12:38.688Z 8 multi - uuid1 uuid3 uuid2 -
2018-06-28T00:35:54.338Z 9 multi - uuid1 uuid2 - -
2018-06-28T00:47:51.679Z 9 multi - uuid1 uuid2 uuid3 -
2018-06-28T00:47:51.720Z 10 multi - uuid1 uuid3 - -
2018-06-28T00:47:58.863Z 10 multi - uuid1 uuid3 uuid2 -
2018-06-28T16:29:01.624Z 10 multi frz uuid1 uuid3 uuid2 -
2018-06-28T17:29:01.624Z 10 multi - uuid1 uuid3 uuid2 -
$ grep -Pzo "^\S+\s+\S+\s+\S+\s+frz\s+.*\n\S+\s+\S+\s\S+\s+\-\s+.*" file
2018-04-09T17:19:01.901Z 8 multi frz uuid1 uuid3 uuid2 -
2018-06-03T22:12:38.688Z 8 multi - uuid1 uuid3 uuid2 -
2018-06-28T16:29:01.624Z 10 multi frz uuid1 uuid3 uuid2 -
2018-06-28T17:29:01.624Z 10 multi - uuid1 uuid3 uuid2 -
$ grep -V 2>&1|head -1
grep (GNU grep) 2.20
$