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:

1
2
printf '%s\n' 'a b c d e f g' '1 2 3 4 5 6 7 8 9 0' 'A B C D E F G' ' ~ ! @ # $ % ^ & * ( ) _ +' > tr_file
cat tr_file
img

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:

1
tr [:lower:] [:upper:] < tr_file
img

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:

1
tr [:upper:] [:lower:] < tr_file
img

Translate uppercase characters into lowercase characters

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:

1
tr -d [:lower:] < tr_file
img

Remove all lowercase characters before outputting

Delete Uppercase Characters

We can use the -d followed by the [:upper:] parameter to remove lowercase letters in the content and then output them:

1
tr -d [:upper:] < tr_file
img

Remove all uppercase characters before outputting

Delete Digital Characters

We can use the -d followed by the [:digit:] parameter to remove digital characters in the content and then output them:

1
tr -d [:digit:] < tr_file
img

Remove all digital characters before outputting

Delete Alphabetic Characters

We can use the -d followed by the [:alpha:] parameter to remove alphabetic letters in the content and then output them:

1
tr -d [:alpha:] < tr_file
img

Remove all alphabetic letters before outputting

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:

1
tr -d [:alnum:] < tr_file
img

Remove all alphabetic letters and digital characters before outputting

Delete Punctuation Characters

We can use the -d followed by the [:punct:] parameter to remove punctuation characters in the content and then output them:

1
tr -d [:punct:] < tr_file
img

Remove all punctuation characters before outputting

Delete Horizontal Spaces

We can use the -d followed by the [:blank:] parameter to remove horizontal spaces in the content and then output them:

1
tr -d [:blank:] < tr_file
img

Remove all horizontal spaces before outputting

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:

1
tr -d [:space:] < tr_file
img

Remove all horizontal and vertical spaces before outputting

References 9.1 tr: Translate, squeeze, and/or delete characters

Buy me a coffeeBuy me a coffee