user17411100
user17411100

Reputation:

BASH sed expression optimisation or conversion to native bash substitution

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

Upvotes: 1

Views: 90

Answers (1)

glenn jackman
glenn jackman

Reputation: 247250

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

Upvotes: 3

Related Questions