I came across a lot of answers, including theses:sed on OSX insert at a certain linehttps://stackoverflow.com/questions/14846304/sed-command-error-on-macos-xhttps://stackoverflow.com/questions/4247068/sed-command-failing-on-mac-but-works-on-linuxhttps://stackoverflow.com/questions/16266281/how-to-add-text-to-the-beginning-of-all-files-in-a-folderAnd I can't find a way to do what I want to do. I need to insert #encoding:utf-8 at the beginning of every .html.erb file of my directory (recursively). I tried using this commandfind . -iname *.erb -type f -exec sed -ie 1i \#encoding:utf-8 {} \;But it throws this error:sed: 1: 1i #encoding:utf-8: extra characters after \ at the end of i command
sed insert in the beginning of multiple files is not working
shell script;sed
To edit file in-place with OSX sed, you need to set empty extension:$ sed -i '' '1i\#encoding:utf-8' filenameAnd you need a literal newline after i\. This is specified by POSIX sed. Only GNU sed allows text to be inserted on the same line with command.sed can also works with multiple files at once, so you can use -exec command {} + form:$ find . -iname *.erb -type f -exec sed -i '' '1i\#encoding:utf-8' {} +