How to print n lines after a matched string from a file using python?
Linux Command grep
abc@xyz:~/Desktop$ grep -A 10 'foo' bar.txt
foo
<shippingcost>
<amount>3.19</amount>
<currency>EUR</currency>
</shippingcost>
<shippingtype>Normal</shippingtype>
<quality>GOOD</quality>
<unlimitedquantity>false</unlimitedquantity>
<isrsl>N</isrsl>
<stock>1</stock>
This command will print 10 lines after the matched string 'foo' from the file bar.txt
Using Python how to do the same thing?
What I tried:
import re
with open("bar.txt") as origin_file:
for line in origin_file:
line= re.findall(r'foo', line)
if line:
print line
The above Python code gives this the following output:
abc@xyz:~/Desktop$ python grep.py
['foo']