I do wish to modify a Mac OS X sandbox file via a one-line (copy and paste) command, by inserting a new line — containing a regex — after a line that contains a specific string (also being a regex pattern).The file to edit requires root rights and is located at /usr/share/sandbox/clamd.sb.Both search and append lines contain loads of usually to be escaped characters because these are regex-es and containing paths.Search for line containing(regex #^/private/var/clamav/)Note: the string is preceded with tabs in one case.Insert this line before the match    (regex #^/System/Library/PrivateFrameworks/TrustEvaluationAgent.framework/Versions/A/TrustEvaluationAgent\$)Note: this to be inserted new-line-string should be prepended with one tab (\t).My failing trysudo sed -i '' -e $'/(regex #\^\/private\/var\/clamav\/)/a \t(regex #\^\/System\/Library\/PrivateFrameworks\/TrustEvaluationAgent\.framework\/Versions\/A\/TrustEvaluationAgent\\\$)' /usr/share/sandbox/clamd.sbsed: 1: /(regex #\^\/private\/ ...: command a expects \ followed by textQuestionHow to fix the above sed command or supply a better readable and working alternative that can be used to copy from a website and paste into the Mac OS X terminal (bash) to extend this sandbox configuration file?
One-liner to insert a new line of text (literally a regex, thus many to be escaped characters) in a configuration file before a specific string?
text processing;command line;sed;macintosh
You can't do this with macOS Sed, because it strips leading whitespace from the lines that you are inserting.Is it portable to indent the argument to sed's 'i\' command?Using Awk:awk '/\(regex #\^\/private\/var\/clamav\/\)/ {print \t(regex #\^/System/Library/PrivateFrameworks/TrustEvaluationAgent.framework/Versions/A/TrustEvaluationAgent\$\)}; {print}' /usr/share/sandbox/clamd.sb > ~/temp-clamd.sbNote that I've redirected the output to ~/temp-clamd.sb rather than editing the file in place (which is tricky or impossible with BSD Awk).Next you can check that the changes are as you expect with:diff /usr/share/sandbox/clamd.sb ~/temp-clamd.sbIf everything is correct, overwrite the contents of the original file with the modified copy (don't use mv, which would change the inode, permissions, owner):cat ~/temp-clamd.sb | sudo tee /usr/share/sandbox/clamd.sb