CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Text Processing / tr

tr

Translate, squeeze or delete characters

Beginnercharacterspipelinescleanup

tr replaces characters with other characters, squeezes repeats (-s) or deletes them (-d). It only reads stdin. Typical jobs: split a list onto lines, upper-case a name, strip the Windows \r that breaks a script, or turn the NUL-separated /proc/PID/environ into readable lines.

Syntaxtr [OPTION]... SET1 [SET2] (input from stdin only)

Watch it run

Turn a comma list into lines, read your PATH one entry per line, and build an env var prefix from a service name.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
echo "web-01,web-02,web-03" | tr ',' '\n'

Every comma becomes a newline: one host per line, ready for while read or xargs.

Anatomy of a command

$
commandtr

Character translator. It works on single characters, never on words or patterns.

How it works

tr builds a 256-entry translation table from the two sets before reading any input, then streams stdin to stdout byte by byte, looking each byte up in the table. Ranges like a-z and classes like [:digit:] or [:space:] expand into lists of characters; when SET2 is shorter than SET1, GNU tr repeats its last character to fill it. -d deletes every byte in SET1, -c complements SET1 (everything NOT in it), and -s squeezes repeated output characters. Because it works on bytes, GNU tr does not handle multi-byte UTF-8 characters: tr can't upper-case é. It also has no idea of words or strings: tr 'abc' 'xyz' maps a to x, b to y and c to z everywhere, which surprises people who expect sed-like replacement. The upside is speed and zero memory: it's the right tool for byte-level cleanup like removing \r or NUL bytes.

Options you'll actually use

SET1 SET2Map each character in SET1 to the one at the same position in SET2
-d SETDelete all characters in SET
-s SETSqueeze runs of a repeated character into one
-c / -CComplement SET1: act on every character NOT in it
a-z, 0-9Ranges of characters
[:upper:] [:lower:]Classes; also [:digit:] [:alpha:] [:alnum:] [:space:] [:punct:]
\n \r \t \0Newline, carriage return, tab, NUL byte

Common mistakes

tr 'http' 'https' < urls.txt

tr maps characters one to one; it can't insert or replace strings. Here h->h, t->t, t->t, p->p: nothing changes.

sed 's|http://|https://|' urls.txt
tr -d '\r' < script.sh > script.sh

The shell truncates script.sh before tr reads it, leaving an empty file.

tr -d '\r' < script.sh > script.tmp && mv script.tmp script.sh # or: dos2unix script.sh
tr [a-z] [A-Z] without quotes

Unquoted brackets are a shell glob: if a file named a or A exists in the directory, the shell replaces the pattern with it.

tr '[:lower:]' '[:upper:]' # or: tr a-z A-Z
echo 'café' | tr '[:lower:]' '[:upper:]' expecting CAFÉ

GNU tr works on bytes, so multi-byte UTF-8 letters are not converted.

echo 'café' | sed 's/.*/\U&/' # or awk '{print toupper($0)}' with gawk

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Print each directory in your PATH on its own line.

  2. 2

    Print ~/app/config/app.env with every letter in upper case.

  3. 3

    Collapse the runs of spaces in the output of df -h so that each column is separated by exactly one space.

  4. 4

    Show the environment of your current shell process from /proc, one variable per line.

Related commands

sedStream editor: find and replace, delete and print linescutCut columns or character ranges out of each linecatPrint and concatenate filesenvShow the environment, or run a command with a changed one
PreviousuniqNextpaste
Linux / Text Processing / tr

tr

Translate, squeeze or delete characters

Beginnercharacterspipelinescleanup

tr replaces characters with other characters, squeezes repeats (-s) or deletes them (-d). It only reads stdin. Typical jobs: split a list onto lines, upper-case a name, strip the Windows \r that breaks a script, or turn the NUL-separated /proc/PID/environ into readable lines.

Syntaxtr [OPTION]... SET1 [SET2] (input from stdin only)

Watch it run

Turn a comma list into lines, read your PATH one entry per line, and build an env var prefix from a service name.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
echo "web-01,web-02,web-03" | tr ',' '\n'

Every comma becomes a newline: one host per line, ready for while read or xargs.

Anatomy of a command

$
commandtr

Character translator. It works on single characters, never on words or patterns.

How it works

tr builds a 256-entry translation table from the two sets before reading any input, then streams stdin to stdout byte by byte, looking each byte up in the table. Ranges like a-z and classes like [:digit:] or [:space:] expand into lists of characters; when SET2 is shorter than SET1, GNU tr repeats its last character to fill it. -d deletes every byte in SET1, -c complements SET1 (everything NOT in it), and -s squeezes repeated output characters. Because it works on bytes, GNU tr does not handle multi-byte UTF-8 characters: tr can't upper-case é. It also has no idea of words or strings: tr 'abc' 'xyz' maps a to x, b to y and c to z everywhere, which surprises people who expect sed-like replacement. The upside is speed and zero memory: it's the right tool for byte-level cleanup like removing \r or NUL bytes.

Options you'll actually use

SET1 SET2Map each character in SET1 to the one at the same position in SET2
-d SETDelete all characters in SET
-s SETSqueeze runs of a repeated character into one
-c / -CComplement SET1: act on every character NOT in it
a-z, 0-9Ranges of characters
[:upper:] [:lower:]Classes; also [:digit:] [:alpha:] [:alnum:] [:space:] [:punct:]
\n \r \t \0Newline, carriage return, tab, NUL byte

Common mistakes

tr 'http' 'https' < urls.txt

tr maps characters one to one; it can't insert or replace strings. Here h->h, t->t, t->t, p->p: nothing changes.

sed 's|http://|https://|' urls.txt
tr -d '\r' < script.sh > script.sh

The shell truncates script.sh before tr reads it, leaving an empty file.

tr -d '\r' < script.sh > script.tmp && mv script.tmp script.sh # or: dos2unix script.sh
tr [a-z] [A-Z] without quotes

Unquoted brackets are a shell glob: if a file named a or A exists in the directory, the shell replaces the pattern with it.

tr '[:lower:]' '[:upper:]' # or: tr a-z A-Z
echo 'café' | tr '[:lower:]' '[:upper:]' expecting CAFÉ

GNU tr works on bytes, so multi-byte UTF-8 letters are not converted.

echo 'café' | sed 's/.*/\U&/' # or awk '{print toupper($0)}' with gawk

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Print each directory in your PATH on its own line.

  2. 2

    Print ~/app/config/app.env with every letter in upper case.

  3. 3

    Collapse the runs of spaces in the output of df -h so that each column is separated by exactly one space.

  4. 4

    Show the environment of your current shell process from /proc, one variable per line.

Related commands

sedStream editor: find and replace, delete and print linescutCut columns or character ranges out of each linecatPrint and concatenate filesenvShow the environment, or run a command with a changed one
PreviousuniqNextpaste