python/sed以在文件中的第一个和最后一次出现模式之间获取数据



我需要通过日志文件解析,然后在该文件中的第一次出现与模式的最后发生之间找到数据

示例:

cat 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

如果文件仅包含两次tom的出现,则可以使用sed

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

但是,正如@andrey指出的那样,情况并非如此。这很丑陋,但再次使用sed

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

您可以使用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

最新更新