从大型text / dat文件中提取字符串/值

时间:2013-01-02 20:44:26

标签: python regex awk grep pattern-matching

我想从文本文件中提取“Drops:XXXXX”,其文本模式如下所示:

Pkts: 215104502  Bytes: 202537648280   Drops: 1302599
Pkts: 55330252  Bytes: 52018951784   Drops: 22086
Pkts: 46226143  Bytes: 42980694784   Drops: 0
Pkts: 52931264  Bytes: 49764764008   Drops: 0
Pkts: 60616843  Bytes: 57773237704   Drops: 1280513
Pkts: 215104502  Bytes: 202537648280   Drops: 1302599.

我对任何模式搜索方法开放(grep,awk,python)

由于

2 个答案:

答案 0 :(得分:3)

几分钟的谷歌搜索会发现你需要正则表达式,而ansewr就像是

re.search(r'(?<=Drops: )\d+', input_string).group(0)

答案 1 :(得分:1)

使用awk,您可以:

awk '{ print $5, $6 }' text