tr command in linux shell

tr command is used to translate the input into another form. tr command allows you to do below things on given data.
  • Replace the characters from given string with other set of characters.
  • Delete specific characters from the input string.
  • Squeeze the occurrences of specific character.
Here are the examples of tr command. You can use below command to convert all upper case characters from the file to lower case.
 
cat myfile | tr [:upper:] [:lower:]

Another way to do same thing is by specifying the range of characters.
 
cat myfile | tr [A-Z] [a-z]

If you want to remove specific set of characters, you can use below command. Here “-d” stands for delete.
 
cat myfile | tr -d [A-C]

To remove spaces, you can use below command.
 
cat myfile | tr -d [:space:]

To remove all digits, you can use below command.
 
cat myfile | tr -d [:digit:]

To complement the output, you can use below command. In below example, everything except digits will be deleted from the file. Here “-c” option stands for complement.
 
cat myfile | tr -cd [:digit:]

To remove repeating occurrences of a character, you can use below command. Below command will replace all repeating occurrences of a by single character – a. Here “-s” stands for squeeze.
 
cat myfile | tr -s [a]

Web development and Automation testing

solutions delivered!!