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

时间:2016-12-15 07:13:05

标签: python regex linux awk 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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

如果文件只包含两次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