import collections
import itertools
import sys
with open('huge-file') as f:
before = collections.deque(maxlen=10)
for line in f:
if 'word' in line:
sys.stdout.writelines(before)
sys.stdout.write(line)
sys.stdout.writelines(itertools.islice(f, 10))
break
before.append(line)
试试这个
使用^{} 在匹配之前最多保存10行,使用^{} 在匹配后获取下10行。在
更新排除ip/mac地址的线路:
^{pr2}$使用
grep
和-C
选项,最简单的解决方案:相关问题 更多 >
编程相关推荐