I want to run a shell script while ignoring some of the commands, because they need privileges. Commands like insmod. So I filter the script with this, and it works (those commands are replaced by true):

sed -e 's/command1/true/g' -e 's/command2/true/g' -e 's/command3/true/g' ... -e 's/commandN/true/g'

Is there a more concise way of expressing that?

With simple caracters, to change 'a', 'b', and 'm' into an 'x' I could do something like:

sed -e 's/[abm]/x/g'


tr abm x

(or its tr abm '[x*]' POSIX equivalent). But with strings?


If your sed is able to use the non-standard -E option to understand extended regular expressions, you may use

sed -E 's/string1|string2|string3/true/g'

The alternation with | is an extended regular expression feature not supported by basic regular expression of the type that sed usually supports.

The sed command above would replace any of the three given string even if they occurred as substrings of other strings (such as string1 in string10).

To prevent that, also match word boundaries on either side:

sed -E 's/\<(string1|string2|string3)\>/true/g'

As an aside, your s/[a|b|m]/x/g expression (now corrected in the question) would not only replace a, b and m with x but would also replace any | in the text with x. This is the same as s/[abm|]/x/g or y/abm|/xxxx/, or the tr command tr 'abm|' 'x'.


Is this what you are looking for? With GNU sed:

sed -E 's/(command1|command2|command3)/true/' file

POSIXly, you could use awk:

awk '{gsub(/command1|command2|command3/, "true"); print}' < file

(sed -E is likely to be specified in the next major of the POSIX standard though, so it will probably spread to most implementations eventually).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.