tr command tutorial

1
tr [OPTION] SET1 [SET2]

replace

1
cat content | tr “[:lower:]” “[:upper:]”

-c

complements the set of characters in string.i.e., operations apply to characters not in the given set

1
echo "my ID is 73535" | tr -cd [:digit:]

Output:

1
73535

-d

delete characters in the first set from the output.

1
echo "Welcome To GeeksforGeeks" | tr -d 'w'

Output:

1
elcome To GeeksforGeeks

-s

replaces repeated characters listed in the set1 with single occurrence

1
echo "Welcome    To    GeeksforGeeks" | tr -s [:space:] ' '

Output:

1
Welcome To GeeksforGeeks

-t

first truncates set1 to the length of set2, then do replacement

1
echo 'the cellar is the safest place' | tr -t abcdefghijklmn 123

output

1
the 3ell1r is the s1fest pl13e

without -t, character in set1 that is after set2 will repeat the last character in set1

1
t33 33331r 3s t33 s133st p3133

tr command tutorial
https://rug.al/2019/2019-04-07-tr-command-tutorial/
Author
Rugal Bernstein
Posted on
April 7, 2019
Licensed under