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 try
sudo sed -i '' -e $'/(regex #"\^\/private\/var\/clamav\/")/a \t(regex #"\^\/System\/Library\/PrivateFrameworks\/TrustEvaluationAgent\.framework\/Versions\/A\/TrustEvaluationAgent\\\$")' /usr/share/sandbox/clamd.sb
sed: 1: "/(regex #"\^\/private\/ ...": command a expects \ followed by text
Question
How 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?
i
to insert before the match, nota
. 2) Avoid picket fence (i.e. use a delimiter like|
- keep in mind that in a context address you have to escape the first one). 3) Withi
ora
you only need to escape backslashes and the end of lines (except the last one):sed -e '\|(regex #"^/private/var/clamav/")|i\' -e ' (regex #"^/System/Library/PrivateFrameworks/TrustEvaluationAgent.framework/Versions/A/TrustEvaluationAgent\\$")'
the leading space in the seconde
xpression is a literal tab.-i ''
until you're sure your command is correct.-i
is for in-place editing, not for testing your command. :)seq 4 | sed '/3/ s/.*/ abc\n&/'
... replace/3/
with appropriate search pattern and ` abc` with line to insert before matching linei
ora
but anyway, even if it wasn't, imo it's still easier to runsed '\|(regex #"^/private/var/clamav/")|s|^| (regex #"^/System/Library/PrivateFrameworks/TrustEvaluationAgent.framework/Versions/A/TrustEvaluationAgent\\$")\n&|'
(replacing then
in\n
with a literal newline) - similar to what Sundeep suggests above - than doing all that escaping withawk
...