I'm using tr to parse text and do several tasks like lower-case all words, remove punctuation and multiple spaces. The end result should be clean lower-case text with only alpha-numeric characters, except in a particular case:


such as


The formulation excepting this case is very simple. For example, just for the case where I remove punctuation, I would substitute it with a space using

tr '[:punct:]' ' '

It's just a matter of using pipes to obtain the rest of the desired output. E.g.,

tr '[:upper:]' '[:lower:]' < $1 | tr '[:punct:]' ' ' | ... > $2

However, I'm having some trouble trying to figure out how to define the exception so that a $ symbol between alphanumeric characters and a group of digits is maintained, while other instances, and all other punctuation symbols, are removed.

  • One possibility is to have a sed filter that first replaces the $ by some outlandish character that isn't a punctuation, run your pipe, and then change the outlandish character backback to $.
    – xenoid
    Commented Apr 25, 2019 at 20:17
  • Is the particular case context-sensitive, that is, should $ only be preserved if it occurs directly between a group of letters and a group of digits? In that case, tr is probably not the best tool for the job. Commented Apr 25, 2019 at 21:00

1 Answer 1


As per the answer from @xenoid, the following code works for me

echo -e "Hello.\n;132\$And; Another\$98?';:" | sed -e 's/\([[:alpha:]]\+\)\$\([[:digit:]]\+\)/\1 THIS \2/g' -e 's/[[:punct:]]//g' -e 's/ THIS /$/g' | tr '[:upper:]' '[:lower:]'

The result is:

132and another$98

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .