grep一个单词,如果发现之前打印10行,模式匹配后打印10行

时间:2016-03-16 05:06:56

标签: python grep

我正在处理一个巨大的文件。我想在行中搜索一个单词,当找到时我应该在模式匹配之前打印10行和10行。我怎么能用Python做到这一点?

5 个答案:

答案 0 :(得分:6)

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)

在匹配前使用collections.deque最多可保存10行,并在匹配后使用itertools.islice获得下一行10条。

更新要排除包含ip / mac地址的行:

import collections
import itertools
import re  # <---
import sys

addr_pattern = re.compile(
    r'\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b|'
    r'\b[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}\b',
    flags=re.IGNORECASE
)  # <--

with open('huge-file') as f:
    before = collections.deque(maxlen=10)
    for line in f:
        if addr_pattern.search(line):  # <---
            continue                   # <---
        if 'word' in line:
            sys.stdout.writelines(before)
            sys.stdout.write(line)
            sys.stdout.writelines(itertools.islice(f, 10))
            break
        before.append(line)

答案 1 :(得分:1)

使用grep -C选项,最简单的解决方案:

grep -C 10 'what_to_search' file.txt

答案 2 :(得分:1)

试试这个

#!/usr/bin/python
import commands

filename = "any filename"
string_to_search = "What you want to search"

extract  = (commands.getstatusoutput("grep -C 10 '%s' %s"%(string_to_search,filename)))[1]

print(extract)

答案 3 :(得分:0)

如何在python中使用这样的短代码进行上下文grepping:

$ cat file2
abcd
xyz
print this 1
print this 2
line having pattern
print this 1
print this 2
abcd
fgg
$ cat p.py 
import re
num_lines_cnt=2
lines=open('file2').readlines()
print([lines[i-num_lines_cnt:i+num_lines_cnt+1] for i in range(len(lines)) if re.search('pattern', lines[i]) is not None])
$ python3 p.py 
[['print this 1\n', 'print this 2\n', 'line having pattern\n', 'print this 1\n', 'print this 2\n']]
$

答案 4 :(得分:0)

无需导入任何程序包,我们就可以实现。

string_to_search=input("Enter the String: ")
before=int(input("How many lines to print before string match ? "))
after=int(input("How many lines to print after string match ? "))
file_to_search=input("Enter the file to search: ")

def search_string(string_to_search, before, after, file_to_search):
    with open(file_to_search) as f:
        all_lines = f.readlines()
        last_line_number=len(all_lines)
        for current_line_no, current_line in enumerate(all_lines):
            if string_to_search in current_line:
                start_line_no=max(current_line_no - before, 0)
                end_line_no=min(last_line_number, current_line_no+after+1)
                for i in range(start_line_no, current_line_no):print(all_lines[i])              
                for i in range(current_line_no, end_line_no):print(all_lines[i])
                break
search_string(string_to_search, before, after, file_to_search)

说明:

string_to_search:您要复制的单词/样式
before:要在模式匹配之前打印的行数
after:图案匹配后要打印的行数
my_file.txt是包含单词/样式/字符串

的文件

current_lineno将包含包含模式

的行号

示例文件内容:

$cat my_file.txt
this is line 1
this is line 2
this is line 3
this is line 4
this is line 5 my pattern is here
this is line 6
this is line 7
this is line 8
this is line 9
this is line 10

示例执行和输出:

$python grep_3.py
Enter the String: my pattern
How many lines to print before string match ? 2
How many lines to print after string match ? 1000
Enter the file to search: my_file.txt
this is line 3

this is line 4

this is line 5 my pattern is here

this is line 6

this is line 7

this is line 8

this is line 9

this is line 10

上面的代码等效于Unix`grep'命令

$ grep -A 2000 -B 2 'my pattern' my_file.txt
this is line 3
this is line 4
this is line 5 my pattern is here
this is line 6
this is line 7
this is line 8
this is line 9
this is line 10