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?
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.
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.
This is wheretrcomes into its own.
Replacing Characters
Thetrcommand operates on its input stream according to rules.
Commands totrusually require two sets of characters.
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.
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.
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 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.
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.
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.
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.
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.
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.
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.
This command substitutes colons “:” for spaces.
The path environment variable is a long string of many directory paths.
A colon “:” separates each path.
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.
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.
There’s not much to learn nor remember.
But its simplicity can be its downfall, too.