Linux tr command

tr (stand for translate), is a command that “translate” (transform) one text into anther.
The structure is:
tr <textToChange> <newText> 
And tr will “translate” (change) textToChange to newText, the rest of the input will just be printed to the screen.

For example to make all lower case upper case:


oren@oren-computer:~/workspace/sass  (master) 
$ cat index.html 
<!DOCTYPE html>
<html>
  <head>
<link rel="stylesheet"  href="style/main.css">

  </head>
  <body>




  </body>

</html>
oren@oren-computer:~/workspace/sass  (master) 
$ cat index.html | tr [a-z] [A-Z]
<!DOCTYPE HTML>
<HTML>
  <HEAD>
<LINK REL="STYLESHEET"  HREF="STYLE/MAIN.CSS">

  </HEAD>
  <BODY>




  </BODY>

</HTML>

The explain of the example, the tr got to parameters, the first [a-z] and that basically mean any lower case letter, this is what we want to change , the second was [A-Z] and that mean that we want to change it to upper case letter.
Because [A-Z] represent all upper case letters.

 

We can use -s option to squeeze characters that repeat themselves.
For example to squeeze any repeated spaces into one space.
In the example below we will squeeze all the space in index.html


oren@oren-computer:~/workspace/sass  (master) 
$ cat index.html | tr -s [:space:]
<!DOCTYPE html>
<html>
 <head>
<link rel="stylesheet" href="style/main.css">
 </head>
 <body>
 </body>
</html>

Advertisements
Tagged

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s