2016-12-15 65 views
-1

我需要通过日志文件进行解析,然后在模式的第一次出现和该文件中最后一次出现的模式之间查找数据Python/sed获取文件中模式的第一次和最后一次出现之间的数据

例如:

猫LOG1(对于图案TOM)

tom dsdsdsd 
ssadsds 
fdfdf 
erdfdf 
df dsfdsd 
sfsfsf 
dsds dsad 
sdsdsd 
tom aasasasa 
da da dad 
sfsfsadadadad 

应该给:

tom dsdsdsd 
ssadsds 
fdfdf 
erdfdf 
df dsfdsd 
sfsfsf 
dsds dsad 
sdsdsd 
tom aasasasa 

回答

0

如果该文件包含的tom只有两次出现,你可以使用sed

sed -n '/tom/,/tom/p' 

然而,随着@And雷伊指出,情况可能并非如此。这很难看,但是再次使用sed

sed -n '/tom/=' file.txt | sed -n '1h;${x;G;s/\n/,/;s/$/p/p}' | xargs -I{} sed -n {} file.txt 
+0

如果模式出现其他情况(例如,在第三个'tom'中插入现有的将在最后一次出现之后打印行。 – Andrey

+0

@Andrey你是对的。我修改了我的答案。 –

+0

这适用于我!谢谢! – supervirus

0

你可以用awk做到这一点(请注意双参数):

awk -v pat='tom' ' 
# save the first and the last occurrences of the pattern 
(ARGIND == 1 && $0 ~ pat){if (!first) first = FNR; last = FNR} 
# output everything between the first and the last occurrences of the pattern 
(ARGIND == 2 && (FRN >= first || FNR <= last)){print $0} 
# skip the remaining lines 
(ARGIND == 2 && FNR > last){exit} 
' log.txt log.txt 

只有两个文件中出现的图案的特殊情况下,这应该是更快:

awk -v pat='tom' ' 
# detect pattern; if the second occurrence, output the line and exit 
($0 ~ pat){if (first++) { print $0 ; exit} } 
# output all lines after the first occurrence 
(first){print $0} 
' log.txt 
相关问题