The text file is like:
<field>
</field>
I want to match the block and write something in between the two field tag. I have got the following code which is from How to search for a string in text files?
!/usr/bin/env python3
import mmap
import os
with open('sample.txt', 'rb+', 0) as file, \
mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s:
if s.find(b'<field>\n<\field>') != -1:
file.write("Hello")
My solution doesn't work even if I use \t to detect the tab
'<field>\n\t<\field>'
I think my issue is how to match multiple lines that have some space or tab in it. Thanks everyone.