Inserting file content into a searched pattern

Hi,
i have to insert the content of source.txt into the searched pattern of the file second.txt.

 
$cat source.txt
One
Two
Three
.
.
 
$cat second.txt
This is second file
pattern match start here
 
pattern match end here
end of the file

so the result will be like this

 
$cat second.txt
This is second file
pattern match start here
One
Two
Three
.
.
pattern match end here
end of the file

i can able to do with help of head and tail with number count. But as the pattern position is not always in fixed line. so believe the AWK is helpful.
Thanks

I think you could use grep with line number to fine the where the pattern starts and ends and use those variable in your head and tail command

Try this:

awk '{print}/pattern/{system("cat source.txt")}' second.txt
1 Like

Hi ,
Is it possible to use the some variable rather than the "source.txt"(as hardcode) so that we can use it as run time variable.
i tried with -v for variable assignment but failed to do so.

read abc
awk -v var="$abc" '{print}/pattern/{system("cat $var")}' second.txt

Should be something like:

read abc
awk -v var="$abc" '{print}/pattern/{system("cat " var)}' second.txt