2016-12-15 70 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 
相關問題