tr只能通过stdin(标准输入),而无法通过命令行参数来接受输入。
格式:tr [option] set1 set2
将来自stdin的输入字符从set1映射到set2,并将其输出写入stdout(标准输出)。
1.将输入字符由大写转成小写:
$echo "HELLO WORLD" | tr 'A-Z' 'a-z'
hello world
2.用tr进行数字加密和解密:
$echo 12345 | tr '0-9' '9876543210'
87654
$echo 87654 | tr '9876543210' '0-9'
12345
3.将制表符转换成空格:
$cat text | tr '\t' ' '
4.用tr删除字符:
$echo "Hello 123 world 456" | tr -d '0-9'
Hello world
5.字符集补集:
$echo hello 1 char 2 next 4 | tr -d -c '0-9 \n'
6.用tr压缩字符:
$ echo "GNU is not UNIX" | tr -s ' '
GNU is not UNIX