我想编写一个高效的 awk 脚本,该脚本将采用类似于下面显示的摘录的文件并从每个匹配的记录中打印某一行(例如,以“时间(UTC):”开头的行)。我相信有比我过去所做的更好的方法来做到这一点。
示例文件(对不起,我不知道如何在代码框中放置空行。它们由“BLANK LINE”表示):
Processor: Some_Proc
Capsule abortion no 32
Time (UTC): Fri Jun 15 06:25:10 2012
CapsuleId: 1704167
CapsuleName: SomeAppProc
Reason: Assertion "Reason1"
BLANK LINE
Processor: Some_Proc
Capsule abortion no 33
Time (UTC): Fri Jun 15 06:25:10 2012
CapsuleId: 1704168
CapsuleName: SomeAppProc
Reason: Assertion "Reason2"
BLANK LINE
Processor: Some_Proc
Capsule abortion no 34
Time (UTC): Fri Jun 15 06:25:10 2012
CapsuleId: 1704168
CapsuleName: SomeAppProc
Reason: Assertion "Reason1"
以前的代码示例(对不起,我不知道如何在这个论坛中保留缩进我尝试了 8 个空格,但是没有用)
BEGIN {
RS="" #Each record is a "paragraph"
FS="\n" #Each field is a line
}
/Reason1/ {
# print $3 would work if it always shows up on the third line
# but the following for loop should find it if it's on a different line
for (i=1;i<=NF;i++) {
if ($i ~ /^Time.*/) {
print $i
next
}
}
}
如果它并不总是以相同的顺序出现,是否有更有效的方法来打印该行?
谢谢