0

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
Tim
  • 41
  • 5
  • The goal is that you add some code of your own to your question to show at least the research effort you made to solve this yourself. – Cyrus Apr 13 '19 at 06:46
  • Since you managed to get it done for one, can you paste the code snippet for the same? – Guru Apr 13 '19 at 06:48
  • Possible duplicate of [Read list of files on unix and run command](https://stackoverflow.com/questions/18028643/read-list-of-files-on-unix-and-run-command) – tripleee Apr 13 '19 at 07:00
  • Sorry my bad, i updated the question with what i tried but not working – Tim Apr 13 '19 at 07:25
  • Do you REALLY want to `add text from 1 file to multiple files between 2 tags` or do you actually just want to append the contents of a file after 1 tag? What should the script do if `/dir/$tag.tar.gz` is present but the next line isn't `end=\`date +%s\``? – Ed Morton Apr 13 '19 at 15:21
  • Yes, the text in the file1.txt is a lot and needs to be inserted to any .sh file between /dir/$tag.tar.gz and end=`date +%s`, – Tim Apr 13 '19 at 15:34
  • So if `/dir/$tag.tar.gz` is present but the next line is not `end=\`date +%s\`` then the contents of `file1.txt` should NOT be inserted, right? – Ed Morton Apr 13 '19 at 15:55
  • To make it easier, just insert content of file1.txt in any .sh file after /dir/$tag.tar.gz tag – Tim Apr 13 '19 at 18:38

1 Answers1

0

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' '{}' +

  • sed: -e expression #1, char 52: unterminated `s' command any idea? i changed my question and its better to insert on a new line after: end=date +%s – Tim Apr 14 '19 at 17:02
  • it seem not to work for subdirectorys but only for files in the same directory, any advice? – Tim Apr 16 '19 at 07:54