4 added 175 characters in body
source | link
printf '%s\n' /PAT/s/PAT/\&\\ \/ - kb ". r insert.txt" j \'b j ,p q | ed -s file.txt
 printf '%s\n' /PAT/s/PAT/\&\\ \/ - kb ". r insert.txt" j \'b j w q | ed -s file.txt
/PAT/s/PAT/&\             #   set address to first line matching PAT and
/                         #   split the line right after PAT
-                         #   set address one line before (return to the line matching PAT)
kb                        #   mark the current line
. r insert.txt            #   insert content of insert.txt after this line         
j                         #   join current line and the next
'b                        #   set  address to marked line (return to the line matching PAT)
j                         #   join current line and the next one
,p                        #   print file content
q                         #   quit editor

Or, without using printf and |:

ed -s file.txt <<< $'/PAT/s/PAT/&\\\n/\n-\nkb\n. r insert.txt\nj\n\'b\nj\nw\nq\n'
printf '%s\n' /PAT/s/PAT/\&\\ \/ - kb ". r insert.txt" j \'b j ,p | ed -s file.txt
 printf '%s\n' /PAT/s/PAT/\&\\ \/ - kb ". r insert.txt" j \'b j w | ed -s file.txt
/PAT/s/PAT/&\             #   set address to first line matching PAT and
/                         #   split the line right after PAT
-                         #   set address one line before (return to the line matching PAT)
kb                        #   mark the current line
. r insert.txt            #   insert content of insert.txt after this line         
j                         #   join current line and the next
'b                        #   set  address to marked line (return to the line matching PAT)
j                         #   join current line and the next one
,p                        #   print file content
printf '%s\n' /PAT/s/PAT/\&\\ \/ - kb ". r insert.txt" j \'b j ,p q | ed -s file.txt
 printf '%s\n' /PAT/s/PAT/\&\\ \/ - kb ". r insert.txt" j \'b j w q | ed -s file.txt
/PAT/s/PAT/&\             #   set address to first line matching PAT and
/                         #   split the line right after PAT
-                         #   set address one line before (return to the line matching PAT)
kb                        #   mark the current line
. r insert.txt            #   insert content of insert.txt after this line         
j                         #   join current line and the next
'b                        #   set  address to marked line (return to the line matching PAT)
j                         #   join current line and the next one
,p                        #   print file content
q                         #   quit editor

Or, without using printf and |:

ed -s file.txt <<< $'/PAT/s/PAT/&\\\n/\n-\nkb\n. r insert.txt\nj\n\'b\nj\nw\nq\n'
3 deleted 7 characters in body
source | link

You could use perl (get the file content and substitute pattern with pattern+file content):

perl -pe '$text=`cat insert.txt`; chomp($text); s/PAT/PAT$text$&$text/' file.txt

add -i to edit in place; g to append after each PAT (pattern) occurrence, e.g.:

perl -i -pe '$text=`cat insert.txt`; chomp($text); s/PAT/PAT$text$&$text/g' file.txt

Another way, using ed:

printf '%s\n' /PAT/s/PAT/\&\\ \/ - kb ". r insert.txt" j \'b j ,p | ed -s file.txt

to edit in-place, replace ,p with w:

 printf '%s\n' /PAT/s/PAT/\&\\ \/ - kb ". r insert.txt" j \'b j w | ed -s file.txt

Probably no one is interested how this works but anyway, printf passes a list of commands to ed:

/PAT/s/PAT/&\             #   set address to first line matching PAT and
/                         #   split the line right after PAT
-                         #   set address one line before (return to the line matching PAT)
kb                        #   mark the current line
. r insert.txt            #   insert content of insert.txt after this line         
j                         #   join current line and the next
'b                        #   set  address to marked line (return to the line matching PAT)
j                         #   join current line and the next one
,p                        #   print file content

You could use perl (get the file content and substitute pattern with pattern+file content):

perl -pe '$text=`cat insert.txt`; chomp($text); s/PAT/PAT$text/' file.txt

add -i to edit in place; g to append after each PAT (pattern) occurrence, e.g.:

perl -i -pe '$text=`cat insert.txt`; chomp($text); s/PAT/PAT$text/g' file.txt

Another way, using ed:

printf '%s\n' /PAT/s/PAT/\&\\ \/ - kb ". r insert.txt" j \'b j ,p | ed -s file.txt

to edit in-place, replace ,p with w:

 printf '%s\n' /PAT/s/PAT/\&\\ \/ - kb ". r insert.txt" j \'b j w | ed -s file.txt

Probably no one is interested how this works but anyway, printf passes a list of commands to ed:

/PAT/s/PAT/&\             #   set address to first line matching PAT and
/                         #   split the line right after PAT
-                         #   set address one line before (return to the line matching PAT)
kb                        #   mark the current line
. r insert.txt            #   insert content of insert.txt after this line         
j                         #   join current line and the next
'b                        #   set  address to marked line (return to the line matching PAT)
j                         #   join current line and the next one
,p                        #   print file content

You could use perl (get the file content and substitute pattern with pattern+file content):

perl -pe '$text=`cat insert.txt`; chomp($text); s/PAT/$&$text/' file.txt

add -i to edit in place; g to append after each PAT (pattern) occurrence, e.g.:

perl -i -pe '$text=`cat insert.txt`; chomp($text); s/PAT/$&$text/g' file.txt

Another way, using ed:

printf '%s\n' /PAT/s/PAT/\&\\ \/ - kb ". r insert.txt" j \'b j ,p | ed -s file.txt

to edit in-place, replace ,p with w:

 printf '%s\n' /PAT/s/PAT/\&\\ \/ - kb ". r insert.txt" j \'b j w | ed -s file.txt

Probably no one is interested how this works but anyway, printf passes a list of commands to ed:

/PAT/s/PAT/&\             #   set address to first line matching PAT and
/                         #   split the line right after PAT
-                         #   set address one line before (return to the line matching PAT)
kb                        #   mark the current line
. r insert.txt            #   insert content of insert.txt after this line         
j                         #   join current line and the next
'b                        #   set  address to marked line (return to the line matching PAT)
j                         #   join current line and the next one
,p                        #   print file content
2 deleted 7 characters in body
source | link

You could use perl (get the file content and substitute pattern with pattern+file content):

perl -pe '$text=`cat insert.txt`; chomp($text); s/PAT/PAT$text/' file.txt

add -i to edit in place; g to append after each PAT (pattern) occurrence, e.g.:

perl -i -pe '$text=`cat insert.txt`; chomp($text); s/PAT/PAT$text/g' file.txt

Another way, using ed:

printf '%s\n' /PAT/s/PAT/PAT\\\&\\ \/ - kb ". r insert.txt" j \'b j ,p | ed -s file.txt

to edit in-place, replace ,p with w:

 printf '%s\n' /PAT/s/PAT/PAT\\\&\\ \/ - kb ". r insert.txt" j \'b j w | ed -s file.txt

Probably no one is interested how this works but anyway, printf passes a list of commands to ed:

/PAT/s/PAT/PAT\&\             #   set address to first line matching PAT and then split
/                         #   split the line right after PAT
-                         #   set address one line before (return to the line matching PAT)
kb                        #   mark the current line
. r insert.txt            #   insert content of insert.txt after this line         
j                         #   join current line and the next
'b                        #   set  address to marked line (return to the line matching PAT)
j                         #   join current line and the next one
,p                        #   print file content

You could use perl (get the file content and substitute pattern with pattern+file content):

perl -pe '$text=`cat insert.txt`; chomp($text); s/PAT/PAT$text/' file.txt

add -i to edit in place; g to append after each PAT (pattern) occurrence, e.g.:

perl -i -pe '$text=`cat insert.txt`; chomp($text); s/PAT/PAT$text/g' file.txt

Another way, using ed:

printf '%s\n' /PAT/s/PAT/PAT\\ \/ - kb ". r insert.txt" j \'b j ,p | ed -s file.txt

to edit in-place, replace ,p with w:

 printf '%s\n' /PAT/s/PAT/PAT\\ \/ - kb ". r insert.txt" j \'b j w | ed -s file.txt

Probably no one is interested how this works but anyway, printf passes a list of commands to ed:

/PAT/s/PAT/PAT\           #   set address to first line matching PAT and then split
/                         #   the line right after PAT
-                         #   set address one line before (return to the line matching PAT)
kb                        #   mark the current line
. r insert.txt            #   insert content of insert.txt after this line         
j                         #   join current line and the next
'b                        #   set  address to marked line (return to the line matching PAT)
j                         #   join current line and the next one
,p                        #   print file content

You could use perl (get the file content and substitute pattern with pattern+file content):

perl -pe '$text=`cat insert.txt`; chomp($text); s/PAT/PAT$text/' file.txt

add -i to edit in place; g to append after each PAT (pattern) occurrence, e.g.:

perl -i -pe '$text=`cat insert.txt`; chomp($text); s/PAT/PAT$text/g' file.txt

Another way, using ed:

printf '%s\n' /PAT/s/PAT/\&\\ \/ - kb ". r insert.txt" j \'b j ,p | ed -s file.txt

to edit in-place, replace ,p with w:

 printf '%s\n' /PAT/s/PAT/\&\\ \/ - kb ". r insert.txt" j \'b j w | ed -s file.txt

Probably no one is interested how this works but anyway, printf passes a list of commands to ed:

/PAT/s/PAT/&\             #   set address to first line matching PAT and
/                         #   split the line right after PAT
-                         #   set address one line before (return to the line matching PAT)
kb                        #   mark the current line
. r insert.txt            #   insert content of insert.txt after this line         
j                         #   join current line and the next
'b                        #   set  address to marked line (return to the line matching PAT)
j                         #   join current line and the next one
,p                        #   print file content
1
source | link