Chapter 5. Redirection, Pipe and Shell Script

Tr (Replace Characters)

Tr (Replace Characters)
Tag:

The tr (TRanslate) command is used to replace a string with another string. As the command doesn't have a file input argument, it is often used with the pipe or redirection.

tr command syntax

The tr command syntax itself is tr ["original string"]["new string"]. The original and new strings should be quoted with " " or ' '; however, the command doesn't work on its own as it needs standard input.

tr command with pipe

To make an input to the tr command, you can use the cat command and connect it with the tr command.

To demonstrate the command, we use a customer purchase data set example below. The file name is purchase.txt and its data fields are, from left to right:

  1. Customer ID
  2. First name
  3. Last name
  4. Product name
  5. Purchase unit
  6. Purchase value
purchase.txt
274,Theresa,Webb,Apple,6,$19.2
357,Guy,Hawkins,Orange,3,$6.6
703,Devon,Lane,Apple,4,$12.8
994,Arlene,McCoy,Apple,4,$12.8
922,Ralph,Edwards,Bananas,3,$12.6
177,Savannah,Nguyen,Apple,6,$19.2
798,Marvin,McKinney,Grapes,7,$53.2
429,Courtney,Henry,Orange,3,$6.6
492,Floyd,Miles,Bananas,2,$8.4
738,Wade,Warren,Grapes,5,$38
130,Jerome,Bell,Grapes,6,$45.6
185,Esther,Howard,Bananas,4,$16.8
426,Robert,Fox,Bananas,6,$25.2
877,Cameron,Williamson,Apple,7,$22.4
556,Annette,Black,Grapes,4,$30.4

Using this data set, if you want to change the delimiter (a symbol that separates strings) from , (comma) to a space, run the following command.

Command Line - INPUT
cat purchase.txt | tr "," " "

You can see the following result in your command line.

Command Line - RESPONSE
274 Theresa Webb Apple 6 $19.2
357 Guy Hawkins Orange 3 $6.6
703 Devon Lane Apple 4 $12.8
994 Arlene McCoy Apple 4 $12.8
922 Ralph Edwards Bananas 3 $12.6
177 Savannah Nguyen Apple 6 $19.2
798 Marvin McKinney Grapes 7 $53.2
429 Courtney Henry Orange 3 $6.6
492 Floyd Miles Bananas 2 $8.4
738 Wade Warren Grapes 5 $38
130 Jerome Bell Grapes 6 $45.6
185 Esther Howard Bananas 4 $16.8
426 Robert Fox Bananas 6 $25.2
877 Cameron Williamson Apple 7 $22.4
556 Annette Black Grapes 4 $30.4

As the standard output is still the command line, you cannot save the result. To save it in a file, run the command below.

Command Line - INPUT
cat purchase.txt | tr "," " " > purchase_s.txt

In the purchase_s.txt file, you'll see the same result as the one in the command line.

tr command with standard input redirection

For the same data set, you can also use standard input redirection to make an input to the tr command as shown below.

Command Line - INPUT
tr "," " " < purchase.txt

If you want to save the result, you can run the command below.

Command Line - INPUT
tr "," " " < purchase.txt > purchase_s.txt

The tr (TRanslate) command is used to replace a string with another string. As the command doesn't have a file input argument, it is often used with the pipe or redirection.

tr command syntax

The tr command syntax itself is tr ["original string"]["new string"]. The original and new strings should be quoted with " " or ' '; however, the command doesn't work on its own as it needs standard input.

tr command with pipe

To make an input to the tr command, you can use the cat command and connect it with the tr command.

To demonstrate the command, we use a customer purchase data set example below. The file name is purchase.txt and its data fields are, from left to right:

  1. Customer ID
  2. First name
  3. Last name
  4. Product name
  5. Purchase unit
  6. Purchase value
purchase.txt
274,Theresa,Webb,Apple,6,$19.2
357,Guy,Hawkins,Orange,3,$6.6
703,Devon,Lane,Apple,4,$12.8
994,Arlene,McCoy,Apple,4,$12.8
922,Ralph,Edwards,Bananas,3,$12.6
177,Savannah,Nguyen,Apple,6,$19.2
798,Marvin,McKinney,Grapes,7,$53.2
429,Courtney,Henry,Orange,3,$6.6
492,Floyd,Miles,Bananas,2,$8.4
738,Wade,Warren,Grapes,5,$38
130,Jerome,Bell,Grapes,6,$45.6
185,Esther,Howard,Bananas,4,$16.8
426,Robert,Fox,Bananas,6,$25.2
877,Cameron,Williamson,Apple,7,$22.4
556,Annette,Black,Grapes,4,$30.4

Using this data set, if you want to change the delimiter (a symbol that separates strings) from , (comma) to a space, run the following command.

Command Line - INPUT
cat purchase.txt | tr "," " "

You can see the following result in your command line.

Command Line - RESPONSE
274 Theresa Webb Apple 6 $19.2
357 Guy Hawkins Orange 3 $6.6
703 Devon Lane Apple 4 $12.8
994 Arlene McCoy Apple 4 $12.8
922 Ralph Edwards Bananas 3 $12.6
177 Savannah Nguyen Apple 6 $19.2
798 Marvin McKinney Grapes 7 $53.2
429 Courtney Henry Orange 3 $6.6
492 Floyd Miles Bananas 2 $8.4
738 Wade Warren Grapes 5 $38
130 Jerome Bell Grapes 6 $45.6
185 Esther Howard Bananas 4 $16.8
426 Robert Fox Bananas 6 $25.2
877 Cameron Williamson Apple 7 $22.4
556 Annette Black Grapes 4 $30.4

As the standard output is still the command line, you cannot save the result. To save it in a file, run the command below.

Command Line - INPUT
cat purchase.txt | tr "," " " > purchase_s.txt

In the purchase_s.txt file, you'll see the same result as the one in the command line.

tr command with standard input redirection

For the same data set, you can also use standard input redirection to make an input to the tr command as shown below.

Command Line - INPUT
tr "," " " < purchase.txt

If you want to save the result, you can run the command below.

Command Line - INPUT
tr "," " " < purchase.txt > purchase_s.txt
Tag: