DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 967,611 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Patrick Wendo
Patrick Wendo

Posted on

Today I Learned about the tr in Unix

tr is a Unix command that is an abbreviation of translate or transliterate, indicating its operation of replacing or removing specific characters in its input data set.

I came across it working on this problem on Codewars. One of the top solutions used the string::count method in ruby, but passed along arguments in a syntax I had not seen before.

In Ruby, to get letters in a range say A to M you could pass, "a".."m", but this will return a range object. However, the String#count method takes in strings. As such str.count("a".."m") would return an error. However, passing along "a-m" would not because the latter uses an implementation of the tr Unix command.

TLDR : Ruby strings utilise an implementation of the tr Unix command allowing us to pass strings like "a-z" and have it unpacked into the entire range without having it as a Range object. Useful in commands that do not take in ranges as arguments.

Top comments (2)

Collapse
 
mccurcio profile image
Matt Curcio

My favorite is:
tr '[:lower:]' '[:upper:]'

This makes some things easier to deal with only upper case rather than both cases.

Collapse
 
w3ndo profile image
Patrick Wendo Author

Oh this is nice.

🌚 Friends don't let friends browse without dark mode.

Just kidding, it's a personal preference. But you can change your theme, font, etc. in your settings.

The more you know. 🌈