Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add text between two patterns in File using sed command

Tags:

linux

unix

sed

I want to add Some large code between two patterns:

File1.txt

This is text to be inserted into the File.

infile.txt

Some Text here
First
Second
Some Text here

I want to add File1.txt content between First and Second :

Desired Output:

Some Text here
First
This is text to be inserted into the File.
Second
Some Text here

I can search using two patterns with sed command ,But I don't have idea how do I add content between them.

sed '/First/,/Second/!d' infile 
like image 802
MaNn Avatar asked May 30 '13 12:05

MaNn


People also ask

How do I print a line between two patterns in Linux?

The sed command will, by default, print the pattern space at the end of each cycle. However, in this example, we only want to ask sed to print the lines we need. Therefore, we've used the -n option to prevent the sed command from printing the pattern space. Instead, we'll control the output using the p command.

How do I add text to sed?

The 'n' command instructs sed to print out each line of the file and print it to the screen. This is not suitable for editing files. Instead, use 'r' or 'w' to append the new data after each line or just write them verbatim to a file.

How do you insert a sed line after a pattern?

You have to use the “-i” option with the “sed” command to insert the new line permanently in the file if the matching pattern exists in the file.

How do I extract text between two words in Unix?

How do I extract text between two words ( <PRE> and </PRE> ) in unix or linux using grep command? Let us see how use the grep command or egrep command to extract data between two words or strings. I also suggest exploring sed/awk/perl commands to extract text between two words on your Linux or Unix machine.


1 Answers

Since /r stands for reading in a file, use:

sed '/First/r file1.txt' infile.txt

You can find some info here: Reading in a file with the 'r' command.

Add -i (that is, sed -i '/First/r file1.txt' infile.txt) for in-place edition.

To perform this action no matter the case of the characters, use the I mark as suggested in Use sed with ignore case while adding text before some pattern:

sed 's/first/last/Ig' file

As indicated in comments, the above solution is just printing a given string after a pattern, without taking into consideration the second pattern.

To do so, I'd go for an awk with a flag:

awk -v data="$(<patt_file)" '/First/ {f=1} /Second/ && f {print data; f=0}1' file

Given these files:

$ cat patt_file
This is text to be inserted
$ cat file
Some Text here
First
First
Second
Some Text here
First
Bar

Let's run the command:

$ awk -v data="$(<patt_file)" '/First/ {f=1} /Second/ && f {print data; f=0}1' file
Some Text here
First                             # <--- no line appended here
First
This is text to be inserted       # <--- line appended here
Second
Some Text here
First                             # <--- no line appended here
Bar
like image 106
fedorqui 'SO stop harming' Avatar answered Oct 11 '22 01:10

fedorqui 'SO stop harming'