Add text between two patterns in a file using sed command

I want to add some great code between two templates:

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 the first and second:

Output Required:

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

I can do a search using two templates with the sed command, but I have no idea how to add content between them.

 sed '/First/,/Second/!d' infile 
+6
source share
4 answers

Since /r means reading in a file, use:

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

You can find the information here: Reading in a file with the "r" command .

Add -i (i.e. sed -i '/First/r file1.txt' infile.txt ) to post in-place.

To perform this action, regardless of the case with characters, use the I sign, as suggested in Use sed with ignore when adding text before some pattern :

 sed 's/first/last/Ig' file 

As stated in the comments, the above solution simply prints the given line after the pattern, not taking into account the second pattern.

To do this, I would select awk with the 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 

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 
+7
source

I think you can try this

 $ sed -n 'H;${x;s/Second.*\n/This is text to be inserted into the File\ &/;p;}' infile.txt 
+1
source

awk flavor:

 awk '/First/ { print $0; getline < "File1.txt" }1' File2.txt 
+1
source

Here's the bash code that I wrote to paste the template from patt_file. Essentially, I had to delete some duplicate data using uniq, and then add some things. I am copying the material that I need to return using the lineNum values, save it in past_file. Then map patMatch in the file to which I am adding stuff.

  #This pulls the line number from row k, column 2 of the reduced repitious file lineNum1=$(awk -vi=$k -vj=2 'FNR == i {print $j}' test.txt) #This pulls the line number from row k + 1, coulmn 2 of the reduced repitious file lineNum2=$(awk -vi=$((k+1)) -vj=2 'FNR == i {print $j}' test.txt) #This pulls fields row 4, 2 and 3 column into with tab spacing (important) from reduced repitious file awk -vi=$k -vj=2 -vh=3 'FNR == i {print $j" "$h}' test.txt>closeJ.txt #This substitutes all of the periods (dots) for \. so that sed will match them patMatch=$(sed 's/\./\\./' closeJ.txt) #This Selects text in the full data file between lineNum1 and lineNum2 and copies it to a file awk -v awkVar1=$((lineNum1 +1)) -v awkVar2=$((lineNum2 -1)) 'NR >= awkVar1 && NR <= awkVar2 { print }' nice.txt >patt_file.txt #This inserts the contents of the pattern matched file into the reduced repitious file #The reduced repitious file will now grow sed -i.bak "/$patMatch/ r "patt_file.txt"" test.txt 
0
source

All Articles