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.
# 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 lengthsThe 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
1. What does the y command do?
2. What happens if the source and destination sets of y have different lengths?
3. Which of these is NOT supported by the y command?
4. The y command is most similar to which Unix utility?
Was this page helpful?
You May Also Like
Substitute Command Flags
The flags that follow the sed substitute command control how many matches are replaced, whether matching is case-insensitive, and what happens after the replacement.
Append, Insert, and Change
The a, i, and c commands let sed add new lines after or before a matched line, or replace matched lines entirely, without relying on the substitute command.
The Hold and Pattern Space
sed maintains two buffers, the pattern space and the hold space, and a handful of commands move data between them to enable multi-line and stateful edits.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics