I've lines of the following form
line=" this is a line with 2 leading spaces and a trailing control char^M"
I want to substitute both the 2 leading spaces and the trailing control char represented here by ^M with nothing.
echo "${line}" | sed 's/^[[:space:]]*//' | tr -dc '[:print:]'
echo "${line}" | sed 's/^[[:space:]]*//' | sed 's/[^[:print:]]//'
both works. I also tried with
echo "${line}" | sed 's/^[[:space:]]*|[^[:print:]]//'
but this doesn't work.
Why doesn't this last expression work?
How can I accomplish this with a single call to sed and a single regex?
What is the preferred solution, for example in terms of efficiency? Is it better to avoid many subshells?
Are there better solutions?