I have a file that has a lot of sets of a few lines of code I need to replace
start
keyword1
stuff
keyword2
end
important stuff in between
start
keyword1
stuff
keyword3
end
I want to replace each set of code (from start, to end, with a single line, call this finalcode)
I am now using:
perl -i -p0e 's/start.*?keyword1.?end/finalcode/s' file
perl -i -p0e 's/start.*?keyword2.?end/finalcode/s' file
perl -i -p0e 's/start.*?keyword3.*?end/finalcode/s' file
but the problem is that the sets of keywords can occur in different order
and they have a similar start line
so if the lines with keyword2 occur first in the file, and there's also a keyword1 set, then it will take the start from keyword2, and wipe the important stuff in between
basically i want each set of code to be replaced by a line of code.
these sets of codes have similar start/ending lines
but i dont know the command to do this
like, when i do:
perl -i -p0e 's/start.*?keyword1.*?end/finalcode/s' file
the keyword should only be 1 line from start, not further down the file (which would mean its in a different line)
im not sure if im explaining this well
thanks for any help!!
---------- Post updated at 01:30 AM ---------- Previous update was at 01:13 AM ----------
or another way to check, when i check for start and keyword1 in the pattern, how can i check that they are close together (e.g., within 50 characters, or 2 lines)? otherwise it does not match the pattern and it should not do the replace