DEV Community

Patrick Wendo
Patrick Wendo

Posted on

2 1

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.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

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

Oh this is nice.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Best practices for optimal infrastructure performance with Magento

Running a Magento store? Struggling with performance bottlenecks? Join us and get actionable insights and real-world strategies to keep your store fast and reliable.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️