Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading