Linux Tip: Use sed to replace characters

Sed is often used to replace words in input, but did you know that you can replace a set of characters with another set of corresponding characters? This is accomplished by use of the ‘y’ command. It’s essentially the same as the ‘tr’ utility and works as follows:

sed 'y/<input_chars>/<replace_chars>/' input_file.txt

An example of replacing all uppercase vowels with lowercase vowels:

sed 'y/AEIOU/aeiou/' input_file.txt

The length of each set of characters must match or the command will result in an error.

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>