Transform

The transform command is peculiar, not only because it is the least mnemonic of all sed commands. This command transforms each character by position in string abc to its equivalent in string xyz.[9] Its syntax follows:

[address]y/abc/xyz/

The replacement is made by character position. Therefore, it has no idea of a “word.” Thus, “a” is replaced by “x” anywhere on the line, regardless of whether or not it is followed by a “b”. One possible use of this command is to replace lowercase letters with their uppercase counterparts.

y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/

This command affects the entire contents of the pattern space. If you want to convert a single word on the input line, you could do it by using the hold space. See Chapter 6 for more details on how to use the hold space. (The process is not trivial: you output the line up to the word you want to change, delete that portion of the line, copy the line after the word to the hold space, transform the word, and then append the contents of the hold space back to the pattern space.)

[9] This command is patterned after the UNIX tr command, which translates characters. This is a useful command in its own right; see your local documentation for details. Undoubtedly sed’s y command would have been named t, if t had not already been taken (by the test command, see Chapter 6).

Get sed & awk, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.