I have an input file something like this:
some line
some other line
another line
start_delimiter
interesting stuff
more interesting stuff
even more interesting stuff
end_delimiter
possibly more stuff
I want to manipulate the lines between start_delimiter and end_delimiter that match a regex pattern and write the results to the input file. For example, add ‘//’ to the beginning of the lines containing the word ‘more’ (as long as those lines are between the delimiters):
some line
some other line
another line
start_delimiter
interesting stuff
//more interesting stuff
//even more interesting stuff
end_delimiter
possibly more stuff
I can get the section of text between the delimiters this way:
awk '/start_delimiter/,/end_delimiter/' inputfile
If I pipe this to another awk I can change the lines I’m interested in:
awk '/more/ {sub(/^/,"//")}1'
What I’m not sure about is how to go back and replace the delimited section with the new contents. Any ideas? Bonus points for a one-liner.
1 Answer