Issue
I need to add text from 1 file to multiple files between 2 tags. all files have same extension .sh but different names and all all in subdirectorys.
i tried modify this but its only working for search and replace:
file=$(cat file1.txt)
replace="s/end=date +%s/$file/g";
find . -type f -name '*.sh' -print0 | xargs -0 sed -i "$replace"
file1.txt
some text here
some text here
some text here
some text here
some text here
final output and content of file1.txt added as new line below end=date +%s to all .sh files
end=date +%s
some text here
some text here
some text here
some text here
some text here
Solution
try gnu sed on bash shell, testing it without -i option;
sed -E '/end=date\s+\+%s/r file1.txt' *.sh
after testing add -i option to real edit e.g. sed -Ei
....
to search files in subs recursively, use find
to do so and the found files are sent to sed
find ~+ -iname '*.sh' -exec sed -E '/end=date\s++%s/r file1.txt' '{}' +
Answered By - user7712945