Generally, we use tr to replace strings or delete specified strings.
The TR syntax is as follows:
Tr [Option]... set1 [set2]
-C,-C, -- Complement
First complement set1
Replace the characters not in set1 with set2
[[email protected] ~]$ echo "lubinsu" | tr -c "l" "A"lAAAAAAA
-D, -- delete
Delete characters in set1, do not translate
Delete characters that contain set1:
[[email protected] ~]$ echo "lubinsu" | tr -d "u"lbins
[[email protected] ~]$ echo "lu123b123ins41u" | tr -d "0-9"lubinsu
-S, -- Squeeze-repeats replace each input sequence of a repeated character that is listed in set1 with a single
Occurrence of that character
Remove duplicate characters and compress them into one character:
[[email protected] ~]$ echo "lubinsu" | tr -c "l\n" "A"lAAAAAA[[email protected] ~]$ echo "lubinsu" | tr -cs "l\n" "A"lA
-T, -- truncate-set1
First truncate set1 to length of set2
The default value is-T:
[[email protected] ~]$ echo "lubinsu" | tr -t "lu" "abc"abbinsb[[email protected] ~]$ echo "lubinsu" | tr "lu" "abc"abbinsb
Other examples:
Case-insensitive replacement:
[[email protected] ~]$ echo "lubinsu" | tr "a-z" "A-Z"LUBINSU[[email protected] ~]$ echo "lubinsu" | tr [:lower:] [:upper:]LUBINSU