100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

The Transform Command

The y command performs character-by-character transliteration, mapping each character in one set to the corresponding character in another, similar to the tr utility.

Editing CommandsBeginner6 min readJul 10, 2026
Analogies

Character-for-Character Translation

The transform command, written y/source/dest/, maps each character in the source set to the character at the same position in the destination set. Unlike substitute, y does not use regular expressions and does no pattern matching whatsoever; it simply swaps individual characters wherever they appear in the pattern space. Because the mapping is positional, the source and destination sets must contain exactly the same number of characters, or sed reports an error and refuses to run. It is sed's built-in equivalent of the tr command.

🏏

Cricket analogy: The y command is like a fixed fielding-position swap card where every named player moves to a predetermined spot; there's no judgement or matching, just a one-to-one reassignment from one list to another.

No Ranges, No Classes

A crucial limitation is that the y command does not support character ranges like a-z or POSIX character classes like [:upper:]. You must spell out every character explicitly in both sets. So to uppercase the alphabet you write out all twenty-six letters in the source and all twenty-six uppercase letters in the destination. Backslash escapes such as \n, \t, and \\ are recognised, and the delimiter itself (usually /) can be changed by escaping or by choosing a different delimiter character after y.

🏏

Cricket analogy: There's no shorthand like 'all the tail-enders': you must name every batsman in the order individually, position by position, because y has no concept of a range or a group.

bash
# Rotate each letter by 13 (ROT13) — full alphabet spelled out
sed 'y/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM/'

# Replace commas with tabs and colons with newlines
sed 'y/,:/\t\n/' data.txt

# ERROR: source has 3 chars, dest has 2 -> sed refuses to run
# sed 'y/abc/xy/'   # strings for 'y' command are different lengths

The source and destination strings of y must be exactly the same length. If they differ, sed aborts with an error like 'strings for y command are different lengths'. Also remember y does NOT accept ranges (a-z) or classes ([:lower:]) — every character must be listed explicitly.

For case conversion, GNU sed offers substitution modifiers \U, \L, and \E in the replacement of an s command, which are often more convenient than spelling out the full alphabet for y. But y remains the portable, POSIX way to do fixed character transliteration.

  • y/source/dest/ maps each character in source to the same-position character in dest.
  • y does no regular-expression matching; it swaps individual characters only.
  • Source and destination sets must be the same length or sed errors out.
  • y supports no ranges (a-z) and no character classes ([:upper:]).
  • Backslash escapes like \n and \t are recognised in the sets.
  • y is sed's POSIX equivalent of the tr command.
  • For case conversion, GNU \U/\L in s/// is often more convenient than y.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#SedStreamEditorStudyNotes#TheTransformCommand#Transform#Command#Character#Translation#StudyNotes#SkillVeris#ExamPrep