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:
|
|
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 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:
|
|
Deleting Characters
Delete Lowercase Characters
We can use the -d followed by the [:lower:] parameter to remove lowercase letters in the content and then output them:
|
|
Delete Uppercase Characters
We can use the -d followed by the [:upper:] parameter to remove lowercase letters in the content and then output them:
|
|
Delete Digital Characters
We can use the -d followed by the [:digit:] parameter to remove digital characters in the content and then output them:
|
|
Delete Alphabetic Characters
We can use the -d followed by the [:alpha:] parameter to remove alphabetic letters in the content and then output them:
|
|
Delete Alphanumeric Characters
We can use the -d followed by the [:alnum:] parameter to remove alphabetic letters and digital characters in the content and then output them:
|
|
Delete Punctuation Characters
We can use the -d followed by the [:punct:] parameter to remove punctuation characters in the content and then output them:
|
|
Delete Horizontal Spaces
We can use the -d followed by the [:blank:] parameter to remove horizontal spaces in the content and then output them:
|
|
Delete Horizontal or Vertical Spaces
We can use the -d followed by the [:space:] parameter to remove horizontal and vertical spaces in the content and then output them:
|
|
References 9.1 tr: Translate, squeeze, and/or delete characters
Author Dong Chen
LastMod Thu Feb 28 2019