I am working on a command that replaces all digits 0-9 with their corresponding letters in sed. I know I'm doing something wrong, but sed is not interpreting the replacement regex as anything but a string literal.

The command I am using is sed -r 's/[0-9]/[A-J]/g' log > ~/output.txt

It seems pretty straightforward to me, but I have been stuck on it for about an hour. The output I receive just replaces 0-9 with the string "[A-J]"


In substitution, only the match (left-hand side) is a regular expression. The replacement is more or less just a literal string (with some backslash expansion) – it's not a regex.

You want transliteration, not substitution, so replace s with y:

echo 34031445 | sed 'y/0123456789/ABCDEFGHIJ/'

sed can't use ranges in y, but Perl can:

echo 34031445 | perl -pe 'y/0-9/A-J/'

Or just use tr:

echo 34031445 | tr 0-9 A-J
  • 12
    Agreed. tr is the easiest and probably the fastest tool for the job.
    – user351764
    Oct 22 '18 at 2:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.