Linux shell tr command details
- 2020-05-17 07:26:39
- OfStack
Linux shell tr command details
1. Use
tr, short for translate, is used to compress duplicate characters, delete control characters in files, and perform character conversion operations.
2. Grammar
tr [OPTION]... SET1 [SET2]
3. The parameters
3.1 -s compressed duplicate characters
replace each input sequence of character repeated is SET1 with with occurrence occurrence that character
xiaosi@Qunar:~/test$ echo "aaabbbaacccfddd" | tr -s [abcdf] // abacfd
You can use this feature to delete the blank lines in the file, essentially replacing the corresponding duplicate characters with the characters specified by SET1, as shown in figure 1 above
xiaosi@Qunar:~/test$ cat b.txt
I like football
Football is very fun!
Hello
xiaosi@Qunar:~/test$ cat b.txt | tr -s ["\n"]
I like football
Football is very fun!
Hello
3.2 -d delete characters
-d: delete, delete all characters specified in SET1, do not convert (delete characters in SET1, do not translate)
xiaosi@Qunar:~/test$ echo "a12HJ13fdaADff" | tr -d "[a-z][A-Z]"
1213
xiaosi@Qunar:~/test$ echo "a1213fdasf" | tr -d [adfs]
1213
3.3 character substitution
-t: truncate, replace the characters in SET1 with the characters corresponding to SET2, and the default value of 1 is -t
xiaosi@Qunar:~/test$ echo "a1213fdasf" | tr -t [afd] [AFO] // A1213FOAsF
The code above converts a to A, f to F, and d to O.
Can use this 1 characteristic, realizes the size letter conversion
xiaosi@Qunar:~/test$ echo "Hello World I Love You" |tr -t [a-z] [A-Z]
HELLO WORLD I LOVE YOU
xiaosi@Qunar:~/test$ echo "HELLO WORLD I LOVE YOU" |tr -t [A-Z] [a-z]
hello world i love you
You can also use character sets for conversion
xiaosi@Qunar:~/test$ echo "Hello World I Love You" |tr -t [:lower:] [:upper:]
HELLO WORLD I LOVE YOU
xiaosi@Qunar:~/test$ echo "HELLO WORLD I LOVE YOU" |tr -t [:upper:] [:lower:]
hello world i love you
Remark:
The set of characters is as follows
\NNN 8 Character of base value NNN (1 to 3 for 8 Character of base value )
\\ The backslash
\a Ctrl-G The bell
\b Ctrl-H Back space
\f Ctrl-L The left page breaks
\n Ctrl-J A new row
\r Ctrl-M enter
\t Ctrl-I tab key
\v Ctrl-X Horizontal tabs
CHAR1-CHAR2 from CHAR1 to CHAR2 All characters according to ASCII Sequence of characters
[CHAR*] in SET2, copies of CHAR until length of SET1
[CHAR*REPEAT] REPEAT copies of CHAR, REPEAT octal if starting with 0
[:alnum:] All the letters and Numbers
[:alpha:] All the letters
[:blank:] Horizontal tabs, whitespace, etc
[:cntrl:] All control characters
[:digit:] All the Numbers
[:graph:] All printable characters, excluding Spaces
[:lower:] All lowercase characters
[:print:] All printable characters, including Spaces
[:punct:] All punctuation characters
[:space:] All horizontal or vertical white space
[:upper:] All capital letters
3.4 character complement substitution
-c: complement, replacing characters not included in SET1 with SET2
xiaosi@Qunar:~/test$ cat a.txt
Monday 09:00
Tuesday 09:10
Wednesday 10:11
Thursday 11:30
Friday 08:00
Saturday 07:40
Sunday 10:00
xiaosi@Qunar:~/test$ cat a.txt | tr -c "[a-z][A-Z]" "#" | tr -s "#" | tr -t "#" "\n"
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
In the code above, tr-c "[a-z][A-Z]" "#" indicates that all characters except the size letters are replaced with #.
The above code can be optimized as:
xiaosi@Qunar:~/test$ cat a.txt | tr -cs "[a-z][A-Z]" "\n"
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Thank you for reading, hope to help you, thank you for your support of this site!