I'm using sed to replace values in other bash scripts, such as:
somedata=`sed 's/ ==/==/g' <<< $somedata` # [space]== becomes ==
somedata=`sed 's/== /==/g' <<< $somedata` # ==[space] becomes ==
sed, you can use the
| (or) operator:
$ sed -r 's/ *(&&|\|\|) */\1/g' <<< "foo && bar || baz" foo&&bar||baz
*(&&|\|\|) *: search for zero or more space followed by any of the
|separated strings followed by zero or more space
As pointed out in comments, you can use the
-E flag with GNU sed in place of
-r. Your command will be more portable:
sed -E 's/ *(\&\&|\|\|) */\1/g'
As GNU sed also supports
\| alternation operator with Basic Regular Expressions, you can use it for better readability:
sed 's/ *\(&&\|||\) */\1/g'