I am processing a huge file. I want to search for a word in the line and when found I should print 10 lines before and 10 lines after the pattern match. How can I do it in Python?
I am processing a huge file. I want to search for a word in the line and when found I should print 10 lines before and 10 lines after the pattern match. How can I do it in Python?
Without importing any package, we can achieve this.
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)
Explanation:
string_to_search: word/pattern that you want to grep
before: number of lines that you want to print before the pattern match
after: number of lines that you want to print after the pattern match
my_file.txt is the file which contains the word/pattern/string
current_lineno will contain the line number which contains the pattern
Sample File Content:
$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
Sample Execution and Output:
$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
The above code is equivalent to Unix `grep' command
$ 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