27. Bash Shell - Text Processing: tr
Contents
We can use the tr command provided by linux to replace or remove text contents.
Translate or Delete Characters
Let’s prepare some test data for the tr command:
|
|

Prints multiple lines of test data to tr_file
Translating Characters
Translate Lowercase Characters to Uppercase
We can use the [:lower:] followed by the [:upper:] parameters to convert lowercase letters into uppercase letters in the content and then output them:
|
|

Translate lowercase characters into uppercase characters
Translate Uppercase Characters to Lowercase
We can use the [:upper:] followed by the [:lower:] parameters to convert lowercase letters into uppercase letters in the content and then output them:
|
|

Translate uppercase characters into lowercase characters
Deleting Characters
Delete Lowercase Characters
We can use the [:lower:] parameter to remove lowercase letters in the content and then output them:
|
|

Remove all lowercase characters before outputting
Delete Uppercase Characters
We can use the [:upper:] parameter to remove lowercase letters in the content and then output them:
|
|

Remove all uppercase characters before outputting
Delete Digital Characters
We can use the [:digit:] parameter to remove digital characters in the content and then output them:
|
|

Remove all digital characters before outputting
Delete Alphabetic Characters
We can use the [:alpha:] parameter to remove alphabetic letters in the content and then output them:
|
|

Remove all alphabetic letters before outputting
Delete Alphanumeric Characters
We can use the [:alnum:] parameter to remove alphabetic letters and digital characters in the content and then output them:
|
|

Remove all alphabetic letters and digital characters before outputting
Delete Punctuation Characters
We can use the [:punct:] parameter to remove punctuation characters in the content and then output them:
|
|

Remove all punctuation characters before outputting
Delete Horizontal Spaces
We can use the [:blank:] parameter to remove horizontal spaces in the content and then output them:
|
|

Remove all horizontal spaces before outputting
Delete Horizontal or Vertical Spaces
We can use the [:space:] parameter to remove horizontal and vertical spaces in the content and then output them:
|
|

Remove all horizontal and vertical spaces before outputting
References 9.1 tr: Translate, squeeze, and/or delete characters
Author Dong Chen
LastMod Thu Feb 28 2019