Today I Learned about the tr in Unix
Patrick Wendo

Patrick Wendo @w3ndo

About: Automating my way through life. :)

Location:
Nairobi, Kenya
Joined:
Sep 23, 2020

Today I Learned about the tr in Unix

Publish Date: May 23 '22
4 2

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.

Comments 2 total

  • Matt Curcio
    Matt CurcioMay 29, 2022

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

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

Add comment