You get two in one today.

Translate

The tr command (short for translate) is used to do simple transformations or clean up of text before piping into the next command.

Examples

Convert mixed-case to lower case.

echo "oNe To Three" | tr 'A-Z' 'a-z'

Convert spaces to tabs.

echo "one to three" | tr ' ' '\t'

Convert comma (CSV) to spaces.

echo "one,to,three" | tr ',' ' '

Redact digits.

echo "Bank acc: 1234-000123456" | tr -d '0-9'

Redact with X.

echo "Bank acc: 1234-000123456" | tr '0-9' 'x'

Cut

The cut command is used to extract fields.

Many configuration files and output from CLI tools form rows of columns. An example can be seen with:

cat /etc/passwd

Each line (row) contains information about a user. The information is divided into columns (or fields) separated by a : character.

We can use cut to extract one or some fields. Like the username for instance.

cat /etc/passwd | cut -d':' -f1

-d':' specify the delimiter (character used to separate fields) to “:”. And -f1 extracts field 1.

Note: field indexes start with 1, not 0.

Btw, the file is called passwd because it used to contain passwords. We’ve latter learned that storing passwords in clear-text in a file accessible by everyone is a bad idea. Modern systems store password hashes in /etc/shadow instead. The hashing algorithm is Yescrypt.

Back to cut.

We can also use it to see what shells users have.

cat /etc/passwd | cut -d':' -f7 | uniq

Some commands have multiple spaces between columns. It which case we can combine tr and cut.

ps aux | tr -s ' ' | cut -d' ' -f1,2

tr -s ' ' “squeeze” consecutive spaces together to one. Then we extract field 1 and 2 with cut -d' ' -f1,2. The result is that we get user and process ID (PID) for all processes on the system.