1

Sorry to edit the question again. I found that I didn't ask my question clearly before.
I asked a question yesterday but I found another problem today /.\
Here is my file:

Time 00:00:01
kkk
lll
ccc
aaa: 88
...
Time 00:00:03
jjj
kkk
lll
ccc
aaa: 89
ooo
bbb
aaa
kkk
lll
ccc
aaa: 90
...
Time 00:00:04
kkk
lll
...

Here is the output I want:

Time 00:00:01
kkk
lll
ccc
aaa: 88
Time 00:00:03
kkk
lll
ccc
aaa: 89
Time 00:00:03
kkk
lll
ccc
aaa: 90

Last time I was looking for one line and the other line above it. This time I am looking for a pattern with multiple lines:

kkk
lll
ccc
aaa: /any thing here/

and a line

Time /any thing here/

From the question I asked yesterday, I tried

awk '/Time/{a=$0}/kkk\nlll\nccc\naaa/{print a"\n"$0}' file

and

perl -ane '$t=$_ if /Time/; print $t,$_ if /kkk\nlll\nccc\naaa/' test2

and

pcregrep.exe -M 'kkk.*(\n|.)lll.*(\n|.)ccc.*(\n|.)*aaa' test2

from this but they are not working or the output is not what I want.

I found a thread like this which is talking about state machine but it is complex since I have several lines to match.

Any suggestion that can solve this problem easily?

Community
  • 1
  • 1
sflee
  • 1,659
  • 5
  • 32
  • 63
  • You are trying to output the set of lines that starts with `kkk` and ends with `aaa` and the `Time` line that immediately proceeds them? – Etan Reisner May 08 '15 at 02:25
  • More than that, `kkk`, `lll`, `ccc` and `aaa` must match at the same time and same order. and the `Time` line proceeds them immediately. – sflee May 08 '15 at 02:33

1 Answers1

1

(This answer was written for the first version of this question.)

$ awk -v RS="Time" -v p="\nkkk\nlll\nccc\naaa" '$0~p {print "Time",$1,p;}' file
Time 00:00:01 
kkk
lll
ccc
aaa
Time 00:00:03 
kkk
lll
ccc
aaa

(This may require GNU awk.)

John1024
  • 109,961
  • 14
  • 137
  • 171
  • I am sorry, I missed a requirement and I edited my post, the requirement is that if there are more than one pattern appear under line `Time`, then I have to print out all of them. How to add this case to the command? Also what is mean by $0~p ? – sflee May 08 '15 at 02:50
  • 1
    The test `$0~p` returns true if the current record, `$0`, contains text that matches the regular expression `p` where `p` is defined earlier on the command line to be the lines that you are looking for. – John1024 May 08 '15 at 02:54
  • It may not need `gnu awk`, it needs `gnu awk` due to multiple characters in `RS` – Jotne May 08 '15 at 05:37