Home > Enterprise >  BASH sed expression optimisation or conversion to native bash substitution
BASH sed expression optimisation or conversion to native bash substitution

Time:09-14

I have a sed expression which might run anywhere from 0 to thousands of times, the input is piped and substituted:

somefunc() { sed "s/\s*//g; s/[\"\'~\!#\\\/\$%\^&\*\(\)\=]//g; s/\.\.//g"; }

And I simply use it like this:

echo 'Hello world' | somefunc

This is quite slow, so I tried to convert to a native bash substitution and failed and I don't know if theres a way for me to optimise it, so I decided to ask here

Is there a way to do this, maybe convert to a native bash substitution, maybe use a different tool, anything that is even slightly faster helps

Thanks in advance

CodePudding user response:

somefunc() {
    local tmp=${1//[[:space:]\"\'~!#\\$%^&*\/()=]/}
    printf '%s' "${tmp//../}"
}
  • Related