Через конечный автомат
text="\
abc1
abc2
Mon, 1 Aug 2012 00:15:00 +0200|1.1.1.1|dnepr1| - |user logged in| -
Mon, 1 Aug 2012 00:15:00 +0200|1.1.1.1|dnepr1| - |user changed password| -
Mon, 1 Aug 2012 00:15:00 +0200|1.1.1.1|dnepr1| - |user logged off| -
def1
def2
Mon, 1 Aug 2012 00:15:00 +xxxx|1.1.1.1|dnepr2| - |user logged in| -
Mon, 1 Aug 2012 00:15:00 +0200|1.1.1.1|dnepr2| - |user logged in| -
Mon, 1 Aug 2012 00:15:00 +0200|1.1.1.1|dnepr2| - |user changed password| -
Mon, 1 Aug 2012 00:15:00 +0200|1.1.1.1|dnepr2| - |user logged off| -
ghi1
ghi2
"
fsm()
{
awk '{
switch (state) {
case 0:
if (/user logged in/) {
out = $0
state = 1
}
break
case 1:
if (/user logged in/) {
out = $0;
} else if (/user changed password/) {
out = out"\n"$0
state = 2
} else {
out = ""
state = 0
}
break
case 2:
if (/user logged off/) {
out = out"\n"$0
print out
out = ""
state = 0
} else {
out = ""
state = 0
}
break
}
}'
}
echo "$text" | fsm
Вывод
[guest@localhost tmp]$ echo "$text" | fsm
Mon, 1 Aug 2012 00:15:00 +0200|1.1.1.1|dnepr1| - |user logged in| -
Mon, 1 Aug 2012 00:15:00 +0200|1.1.1.1|dnepr1| - |user changed password| -
Mon, 1 Aug 2012 00:15:00 +0200|1.1.1.1|dnepr1| - |user logged off| -
Mon, 1 Aug 2012 00:15:00 +0200|1.1.1.1|dnepr2| - |user logged in| -
Mon, 1 Aug 2012 00:15:00 +0200|1.1.1.1|dnepr2| - |user changed password| -
Mon, 1 Aug 2012 00:15:00 +0200|1.1.1.1|dnepr2| - |user logged off| -
[guest@localhost tmp]$