Do you need a no-frills method for manipulating a stream of text in Linux?

This is how it’s done.

What Is the tr Command?

Ubuntu Linux start up screen on a laptop

Jordan Gloor / How-To Geek

As we all know, Linux is an open-source rewrite of Unix.

It adds its own stuff into the mix, too.

All Linux distributions, at least in their core utilities, adhere to the Unix philosophy.

Replacing a single character with tr

The Unix philosophyencapsulates the visionthe Unix pioneers had for their newoperating system.

It’s often para-phrased as being “Write programs that do one thing well.”

But there’s more to it than that.

Removing two letters with tr

This is wheretrcomes into its own.

Replacing Characters

Thetrcommand operates on its input stream according to rules.

Commands totrusually require two sets of characters.

Using unbalanced sets of characters with tr

The first set holds the characters that will be replaced if they are found in the input stream.

The second set holds the characters that they will be replaced with.

We’reusing

to push some text intotr.

Using unbalanced sets of characters with tr with the truncate option

For this to work you must have the same number of characters in both sets.

If you don’t, you’ll get predictable, but probably unwanted, behavior.

There are more characters in set one than in set two.

Using character ranges to convert a string to uppercase

The letters “d” to “m” have no corresponding character in set two.

They’ll still get replaced, but they’re all replaced with the last character in set two.

This only replaces those characters contained in set one that have a matching character in set two.

Using character ranges to convert a string to lowercase

Using Ranges and Tokens

Set one and set two can contain ranges of characters.

We can make use of this to change the case of a stream of text.

This will convert the input stream to uppercase.

Using tokens to change the case of strings of text

We can perform our lowercase to uppercase and uppercase to lowercase conversions just as easily, using tokens.

This command converts everything apart from the letter “c” to a hyphen “-”.

This command adds the letter “a” to the first set.

Replacing all but the specified character with other characters

Anything apart from “a” or “c” is converted to a hypen “-” character.

Deleting and Squeezing Characters

We can usetrto remove characters altogether, without any replacement.

This is one instance where we only have one set of characters on the command line, not two.

Replacing all but the specified multiple characters with other characters

Another is when we use the-s(squeeze-repeats) option.

This option reduces repeated characters to a single character.

This example will reduce repeated sequences of the space character to a single space.

Deleting multiple characters from a string of text with tr

Any that it finds are removed.

The spaces are deleted.

Note that we get a newline after the output stream is written in the terminal window.

Changing repeated sequences of characters to a single occurrence of the character, with tr

This is because[:space:]includes newlines.

Any spaces, tabs, and newline characters are removed from the input stream.

Of course, you could use an actual space character as well.

Changing repeated sequences of characters to a single occurrence of the character, with tr

We can just as easily delete digits.

By combining the-c(complement) and-d(delete) options we can delete everything apart from digits.

We can change the delimiter that separates words, too.

Deleing blanks (space characters) froma string of text, with tr

This command substitutes colons “:” for spaces.

The path environment variable is a long string of many directory paths.

A colon “:” separates each path.

Deleting whitespace from a string of text, with tr

We’ll change them to newline characters.

That’s much easier to visually parse.

If we have output that we want to reformat into a single line, we can do that too.

Deleting spaces from a string of text, by specifying a space character, with tr

The file “lines.txt” contains some text, with one word on each line.

We’ll feed that intotrand convert it to a single line.

This command usestrfour times.

Deleting digits from a string of text with tr

There’s not much to learn nor remember.

But its simplicity can be its downfall, too.

Deleting everything apart from digits from a string of text using tr

Splitting a line of text into one word per line, with tr

Changing the word delimiter from spaces to colons, with tr

Splitting the $PATH environment variable into separate directory paths, one per line, with tr

Combining multi-line input into a single line of text, using tr

A pipeline of four instances of tr