Tr

tr stands for translate and is a linux command line tool that allows us to transform characters in text provided to tr, either from files or from the output of other commands. Lets look at a very basic example of tr:

echo "Hello there world!" | tr 'e' 'E'
HEllo thErE world!

When we use tr we provide it the character(s) we want to search for and the character(s) we want to replace it with. It is very important to note that tr is used to translate characters. Lets see what I mean by that:

echo "Hello there world! What a beautiful day!" | tr 'aeiou' 'AEIOU'
HEllO thErE wOrld! WhAt A bEAUtIfUl dAy!

You can see that tr translated every a, e, i, o, or u character to its upper case counterpart.

Deleting

We can also use tr to delete characters from our text using the -d flag. Lets see that in action:

echo "Hello there world! What a beautiful day!" | tr -d 'aeiou '
Hllthrwrld!Whtbtfldy!

Now we removed every vowel and space from our text.

Squeeze

Other than the default translate function or the delete function of tr we also have a squeeze function available to us. This squeeze function is signified with the -s flag. The squeeze function will go and find repeated characters matching the list we supply and squeezing them down to one character. Lets see that squeeze action:

echo "Thiiiiisss is aaaaa llllliinneeee offfff texxxxxtttt." | tr -s 'aeiou'
Thisss is a lllllinne offfff texxxxxtttt.

You can see we squeezed the duplicate vowels down to a single character. We can also provide tr with a class of characters as opposed to a large list. Lets try this on the previous echo statement we made:

echo "Thiiiiisss is aaaaa llllliinneeee offfff texxxxxtttt." | tr -s '[:lower:]' '[:upper:]'
THIS IS A LINE OF TEXT.

This command squeezed any lower case letters for us and turned them into upper case letters.

Complement

Another function of tr is the complement function. We can think of complement as a function we can add onto any tr command to do the opposite of what we say. Lets see complement paired with a delete command:

echo "Hello there world! What a beautiful day!" | tr -cd 'aeiou '
eo ee o a a eauiu a

This deleted everything but the vowels from our text. It is worth noting that the end of our text has a endline character (\n) and this will be deleted as well unless we add it to our list of characters.

echo "Hello there world! What a beautiful day!" | tr -cd 'aeiou \n'
eo ee o a a eauiu a

That concludes all of the basic features of tr I am going to cover. If you feel like you want to know more ways to use tr I have included some useful tricks below.

Useful Tricks

Here are some useful ways to use tr in your scripting life.

Random text

If you didn't already know there is a file in linux that contains random text in it (/dev/urandom). Grabbing text from this file can be handy for generating random passwords. If we run head on it though we will see that it also contains gibberish:

head -n 3 /dev/urandom

We can pipe this into tr and delete everything that isn't a printable character like so:

head -n 3 /dev/urandom | tr -cd '[:print:]'
L4OR+rJ+"f~"eh4Oc.,&{6VvV;rWX%Q/*Uv;WzhBmt0 9Cen13R!%yzDp\p4@Sk{P(sjVZ@;57ZC;yzy8,qu5ve]j9~z9t62Q,*dY*d^\e[r41KSy~E/]DHxx9r62i/H[-,jG[xw<z!H6GRhfjQ,9ru-V*(q'i!hIw+fyKo=rH*:K_R&xj du5SwI`frf]_yKMWT* Dsg[H}&&x{%rG/!c~dKF-;<kQ\:(9~w0\$1SJ hhx7# lcW%t1a',rl@ibDy'7bOA>B%*%1_t_ln{K+mL|Do'*dy@[u5Y&yX,b1VDIUJ1Ic.-TSs(4?U[}a8Y^CoHr2%>AG0 L<*prZp]wUz(IdDk"nKBk"-270:y6OFNe5hd/?Zt=c2xH!p(13wmm`BA*DAohx5#pfwZEZH[\2SL6Dj/&67Lx*DYZudC)J:|Zxr<:;ZBf;QhUo9ol^!q:}r}"c$m`T#eGBoklARqSn~1ziI(@v`.HwfR MSN|?IEsyP>v#<Va,xJ]Z-CIawlQJ(_%feAB|dz`&NXe5By7z6Bikv`GS:H~'l?Z?P[rK&geF=P.Tn.uP=kg2ab@*{YzhR zQHrVQ1i6D#,ReE+(kj4zMqen]:v53*F?&;4t;^'H#fDv+;k7geP\Q -4Jpc=?_nM+boK8n\.kj(f9=z{t*4[`E?"N@xm'Y_VF]s~@xF`m@{uHC4^:X_#94^?}n hw_DA;oeTt=>:jEIL:gR9t&;MlT0.-QnfJzQ%*E>I_(.6%8s/h'Z)I7dT,VTk

Random digits

We can also do something similar to the above trick to give us random digits:

head -n 3 /dev/urandom | tr -cd '[:digit:]'
24919979389
This page was last updated: 2024-11-06 Wed 14:16. Source