Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting multiline text from file to another file after pattern

Tags:

bash

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

like image 201
kasper Avatar asked Feb 22 '11 00:02

kasper


People also ask

How do I add multiple lines to a file?

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.

How do you write a multi line command in a shell script?

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.

What is cat << EOF?

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.


1 Answers

sed -e '/pattern/r text2insert' filewithpattern
like image 114
Dennis Williamson Avatar answered Sep 24 '22 20:09

Dennis Williamson