1) Please use
[code][/code] tags around your code, to preserve formatting and to improve readability.
2) It's generally better to provide real-life examples of the text to be modified than simplified mock-up versions. Are there any reserved characters in the text we may have to account for, for example? Also, how big is the file? What is its source? It's faster and easier to devise useful solutions when we know exactly what we're dealing with.
3) For the same reason you should also tell us about all the possible variations in the text patterns to match. Can pattern1 also appear more than once, for example? Or appear alone without pattern2 appearing? Can the pattern1..pattern2 block appear more than once? What do you want to happen in any of these cases?
The main tools used for text manipulation are sed and awk.
Assuming the above simple case is representative, then sed can do it:
Code:
sed '/pattern1/,/pattern2/ { /pattern2/i new line1\nnew line2
}' file
When the command matches a line containing pattern1, it continues to match until it finds a line with pattern2. Then it matches within that block for pattern2 (the last line) and inserts the required text in front of it. Note that sed requires a literal line break after the inserted text in this case, in order to know where the inserted text ends and the command continues.
Here are a few useful sed and awk references.
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.informatik.uni-hamburg.de.../gawk_toc.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/