Skip to content
Latchkey

sed y/// to Transliterate Characters

sed y/abc/xyz/ replaces each character in the first set with the character at the same position in the second.

When you need a one-to-one character swap rather than a pattern match, y is sed answer to tr, and it works inside the same program as your other edits.

What it does

The y command transliterates: each character in the source set maps to the character at the same index in the destination set. Both sets must be the same length. Unlike s, y does no pattern matching and has no flags or regex.

Common usage

Terminal
# uppercase a fixed alphabet
echo "abc" | sed 'y/abc/ABC/'          # ABC

# swap two characters throughout
echo "a-b-c" | sed 'y/-/_/'            # a_b_c

# map digits to letters
echo "123" | sed 'y/123/xyz/'          # xyz

Options

AspectDetail
y/src/dst/Map each src char to the dst char at the same index
equal lengthsrc and dst must contain the same number of chars
no regexy does not interpret regex or ranges like a-z
\n, \t, \\Escapes are allowed inside the sets
delimiterLike s, the char after y sets the separator

In CI

y is portable between GNU and BSD sed and useful for fixed swaps such as turning hyphens into underscores in a generated name. For full case conversion of arbitrary letters, tr [:lower:] [:upper:] or the GNU \U escape is simpler, since y needs every letter spelled out.

Common errors in CI

sed: -e expression #1, char N: strings for `y' command are different lengths means the two sets differ in length; every source character needs exactly one destination character. y does not expand a-z, so write the characters out or use tr.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →