How to instruct sed to substitute only once while using multiple substitute expressions?
Use the t
command after each s
command to branch to the end of the script if a substitution was made:
sed -e 's/something/else/;t' \
-e 's/one/two/;t' \
-e 's/two/three/;t' <<<"one"
Here, the t
command after the last substitution is not needed, but if you generate this code automatically, there is no problem letting it suffix each s
, even the last.
sed with branch 't' in other command is the perfect answer.
This is just a suggestion , in case if you want to do multiple stuff inside if its replaced , you can use awk:
awk ' { if (gsub("something","else",$0) || gsub("one","two",$0) || gsub("two","three",$0) ){ print $0;exit}; }' <<<"one"