I have files like:
text2insert
filewithpattern
and also known:
pattern
How can i insert lines from text2insert
into filewithpattern
but after the pattern
line?
Using bash 2.05b
UPDATE:
Before filewithpattern
should look like:
garbage
pattern1
pattern2
garbage
and after:
garbage
pattern1
text2insert lines
text2insert lines
text2insert lines
pattern2
garbage
To add multiple lines to a file with echo, use the -e option and separate each line with \n. When you use the -e option, it tells echo to evaluate backslash characters such as \n for new line. If you cat the file, you will realize that each entry is added on a new line immediately after the existing content.
Using a Backslash. The backslash (\) is an escape character that instructs the shell not to interpret the next character. If the next character is a newline, the shell will read the statement as not having reached its end. This allows a statement to span multiple lines.
This operator stands for the end of the file. This means that wherever a compiler or an interpreter encounters this operator, it will receive an indication that the file it was reading has ended.
sed -e '/pattern/r text2insert' filewithpattern
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With