Tr command usage
https://www.cnblogs.com/bingguoguo/articles/9188703.html
tr command can replace characters from standard input, compression and deletion. It can be turned into a set of characters to another set of characters, often used to write beautiful one-line command, the role is very powerful.
grammar
tr (option) (parameters)
Options
-c or --complerment: replace all the characters are not the first character set; -d or --delete: delete all characters belonging to a first character set; -s or --squeeze-repeats: the characters are continuously repeated It represents a single character; -t or --truncate-set1: delete the characters in the first character set than the second extra character set.
parameter
- Character Set 1: Specifies the original character set to be converted or deleted. When the shift operation is performed, you must use parameter "Character Set 2" target character set specified transformation. But when a delete operation, no parameter "Character Set 2";
- Character Set 2: designated to be converted to the target character set.
Examples
The input character converted from uppercase to lowercase:
echo "HELLO WORLD" | tr 'A-Z' 'a-z'
hello world
'AZ' and 'a-z' are set, can develop their own set, for example: 'ABD -}', 'bB,.', 'A-de-h', 'a-c0-9' are belonging to the collection, the collection can use '\ n', '\ t', you can use other ASCII characters.
Use delete characters tr:
echo "hello 123 world 456" | tr -d '0-9' hello world
The tabs are converted to spaces:
cat text | tr '\t' ' '
Character sets complement, not set up all the characters will be deleted from the input text:
echo aa.,a 1 b#$bb 2 c*/cc 3 ddd 4 | tr -d -c '0-9 \n' 1 2 3 4
In this example, make up set contains the numbers 0 to 9, spaces and line breaks \ n, it is not deleted, all the other characters are deleted.
Tr compression with characters, can compress the input characters repeated:
echo "thissss is a text linnnnnnne." | tr -s ' sn' this is a text line.
Clever use tr to do the operation numbers together:
echo 1 2 3 4 5 6 7 8 9 | xargs -n1 | echo $[ $(tr '\n' '+') 0 ]
Remove Windows Files "cause" of the '^ M' character:
cat file | tr -s "\r" "\n" > new_file
或
cat file | tr -d "\r" > new_file
tr character classes that can be used:
[: alnum:]: letters and numbers [: alpha:]: letters [: cntrl:]: control (non-printing) characters [: digit:]: Digital [: graph:]: graphic character [: lower:]: Lowercase letters [: print:]: printable characters [: punct:]: punctuation [: space:]: whitespace characters [: upper:]: uppercase letters [: xdigit:]: hexadecimal characters
Use:
tr '[: lower:]' '[: upper:]'
generates fixed-length random password
head /dev/urandom | tr -dc A-Za-z0-9 | head -c 20
tr command can replace characters from standard input, compression and deletion. It can be turned into a set of characters to another set of characters, often used to write beautiful one-line command, the role is very powerful.
grammar
tr (option) (parameters)
Options
-c or --complerment: replace all the characters are not the first character set; -d or --delete: delete all characters belonging to a first character set; -s or --squeeze-repeats: the characters are continuously repeated It represents a single character; -t or --truncate-set1: delete the characters in the first character set than the second extra character set.
parameter
- Character Set 1: Specifies the original character set to be converted or deleted. When the shift operation is performed, you must use parameter "Character Set 2" target character set specified transformation. But when a delete operation, no parameter "Character Set 2";
- Character Set 2: designated to be converted to the target character set.
Examples
The input character converted from uppercase to lowercase:
echo "HELLO WORLD" | tr 'A-Z' 'a-z'
hello world
'AZ' and 'a-z' are set, can develop their own set, for example: 'ABD -}', 'bB,.', 'A-de-h', 'a-c0-9' are belonging to the collection, the collection can use '\ n', '\ t', you can use other ASCII characters.
Use delete characters tr:
echo "hello 123 world 456" | tr -d '0-9' hello world
The tabs are converted to spaces:
cat text | tr '\t' ' '
Character sets complement, not set up all the characters will be deleted from the input text:
echo aa.,a 1 b#$bb 2 c*/cc 3 ddd 4 | tr -d -c '0-9 \n' 1 2 3 4
In this example, make up set contains the numbers 0 to 9, spaces and line breaks \ n, it is not deleted, all the other characters are deleted.
Tr compression with characters, can compress the input characters repeated:
echo "thissss is a text linnnnnnne." | tr -s ' sn' this is a text line.
Clever use tr to do the operation numbers together:
echo 1 2 3 4 5 6 7 8 9 | xargs -n1 | echo $[ $(tr '\n' '+') 0 ]
Remove Windows Files "cause" of the '^ M' character:
cat file | tr -s "\r" "\n" > new_file
或
cat file | tr -d "\r" > new_file
tr character classes that can be used:
[: alnum:]: letters and numbers [: alpha:]: letters [: cntrl:]: control (non-printing) characters [: digit:]: Digital [: graph:]: graphic character [: lower:]: Lowercase letters [: print:]: printable characters [: punct:]: punctuation [: space:]: whitespace characters [: upper:]: uppercase letters [: xdigit:]: hexadecimal characters
Use:
tr '[: lower:]' '[: upper:]'
generates fixed-length random password
head /dev/urandom | tr -dc A-Za-z0-9 | head -c 20