linux命令之tr
linux命令tr用于转换或删除文件中的字符。tr命令只能对标准输入进行操作,不能直接对文件进行操作
tr [参数] [标准输入]
参数 | 说明 |
-s | 将多个连续的字符压缩为一个字符 |
-d | 删除指定的字符 |
-t | 对指定的字符进行替换 |
-c | 将指定字符外的字符替换为指定字符 |
命令:
tr -s abcd < zzz.txt
[root@rhel77 ~]# cat zzz.txt
123acb111 aaaa bbbb
123acb111 error 11111
this is a test line
aaa bbb ccc
this is a test line
aaaa bbbb cccc
1 111 111
2 222 222
1 111 111
aaa
a 1[34]5 dd
a 135 dd
a 145 dd
loveable aaaa
[root@rhel77 ~]# tr -s abcd < zzz.txt
123acb111 a b
123acb111 error 11111
this is a test line
a b c
this is a test line
a b c
1 111 111
2 222 222
1 111 111
a
a 1[34]5 d
a 135 d
a 145 d
loveable a
[root@rhel77 ~]#
命令:
cat zzz.txt | tr -s a A
[root@rhel77 ~]# cat zzz.txt
123acb111 aaaa bbbb
123acb111 error 11111
this is a test line
aaa bbb ccc
this is a test line
aaaa bbbb cccc
1 111 111
2 222 222
1 111 111
aaa
a 1[34]5 dd
a 135 dd
a 145 dd
loveable aaaa
[root@rhel77 ~]# cat zzz.txt | tr -s a A
123Acb111 A bbbb
123Acb111 error 11111
this is A test line
A bbb ccc
this is A test line
A bbbb cccc
1 111 111
2 222 222
1 111 111
A
A 1[34]5 dd
A 135 dd
A 145 dd
loveAble A
[root@rhel77 ~]#
命令:
cat zzz.txt | tr -s ab AB
[root@rhel77 ~]# cat zzz.txt
123acb111 aaaa bbbb
123acb111 error 11111
this is a test line
aaa bbb ccc
this is a test line
aaaa bbbb cccc
1 111 111
2 222 222
1 111 111
aaa
a 1[34]5 dd
a 135 dd
a 145 dd
loveable aaaa
[root@rhel77 ~]# cat zzz.txt | tr -s ab AB
123AcB111 A B
123AcB111 error 11111
this is A test line
A B ccc
this is A test line
A B cccc
1 111 111
2 222 222
1 111 111
A
A 1[34]5 dd
A 135 dd
A 145 dd
loveABle A
[root@rhel77 ~]#
命令:
cat zzz.txt | tr -s "\n"
[root@rhel77 ~]# cat zzz.txt
123acb111 aaaa bbbb
123acb111 error 11111
this is a test line
aaa bbb ccc
this is a test line
aaaa bbbb cccc
1 111 111
2 222 222
1 111 111
aaa
a 1[34]5 dd
a 135 dd
a 145 dd
loveable aaaa
[root@rhel77 ~]# cat zzz.txt | tr -s "\n"
123acb111 aaaa bbbb
123acb111 error 11111
this is a test line
aaa bbb ccc
this is a test line
aaaa bbbb cccc
1 111 111
2 222 222
1 111 111
aaa
a 1[34]5 dd
a 135 dd
a 145 dd
loveable aaaa
[root@rhel77 ~]#
命令:
cat zzz.txt | tr -d a
[root@rhel77 ~]# cat zzz.txt
123acb111 aaaa bbbb
123acb111 error 11111
this is a test line
aaa bbb ccc
this is a test line
aaaa bbbb cccc
1 111 111
2 222 222
1 111 111
aaa
a 1[34]5 dd
a 135 dd
a 145 dd
loveable aaaa
[root@rhel77 ~]# cat zzz.txt | tr -d a
123cb111 bbbb
123cb111 error 11111
this is test line
bbb ccc
this is test line
bbbb cccc
1 111 111
2 222 222
1 111 111
1[34]5 dd
135 dd
145 dd
loveble
[root@rhel77 ~]#
命令:
cat zzz.txt | tr -d ab
[root@rhel77 ~]# cat zzz.txt
123acb111 aaaa bbbb
123acb111 error 11111
this is a test line
aaa bbb ccc
this is a test line
aaaa bbbb cccc
1 111 111
2 222 222
1 111 111
aaa
a 1[34]5 dd
a 135 dd
a 145 dd
loveable aaaa
[root@rhel77 ~]# cat zzz.txt | tr -d ab
123c111
123c111 error 11111
this is test line
ccc
this is test line
cccc
1 111 111
2 222 222
1 111 111
1[34]5 dd
135 dd
145 dd
lovele
[root@rhel77 ~]#
命令:
cat zzz.txt | tr -d abc
[root@rhel77 ~]# cat zzz.txt
123acb111 aaaa bbbb
123acb111 error 11111
this is a test line
aaa bbb ccc
this is a test line
aaaa bbbb cccc
1 111 111
2 222 222
1 111 111
aaa
a 1[34]5 dd
a 135 dd
a 145 dd
loveable aaaa
[root@rhel77 ~]# cat zzz.txt | tr -d abc
123111
123111 error 11111
this is test line
this is test line
1 111 111
2 222 222
1 111 111
1[34]5 dd
135 dd
145 dd
lovele
[root@rhel77 ~]#
命令:
cat zzz.txt | tr -t abc ABC
其中:-t可以省略
[root@rhel77 ~]# cat zzz.txt
123acb111 aaaa bbbb
123acb111 error 11111
this is a test line
aaa bbb ccc
this is a test line
aaaa bbbb cccc
1 111 111
2 222 222
1 111 111
aaa
a 1[34]5 dd
a 135 dd
a 145 dd
loveable aaaa
[root@rhel77 ~]# cat zzz.txt | tr -t abc ABC
123ACB111 AAAA BBBB
123ACB111 error 11111
this is A test line
AAA BBB CCC
this is A test line
AAAA BBBB CCCC
1 111 111
2 222 222
1 111 111
AAA
A 1[34]5 dd
A 135 dd
A 145 dd
loveABle AAAA
[root@rhel77 ~]#
命令:
cat zzz.txt | tr -t a-z A-Z
[root@rhel77 ~]# cat zzz.txt
123acb111 aaaa bbbb
123acb111 error 11111
this is a test line
aaa bbb ccc
this is a test line
aaaa bbbb cccc
1 111 111
2 222 222
1 111 111
aaa
a 1[34]5 dd
a 135 dd
a 145 dd
loveable aaaa
[root@rhel77 ~]# cat zzz.txt | tr -t a-z A-Z
123ACB111 AAAA BBBB
123ACB111 ERROR 11111
THIS IS A TEST LINE
AAA BBB CCC
THIS IS A TEST LINE
AAAA BBBB CCCC
1 111 111
2 222 222
1 111 111
AAA
A 1[34]5 DD
A 135 DD
A 145 DD
LOVEABLE AAAA
[root@rhel77 ~]#
命令:
cat zzz.txt | tr -c a X
[root@rhel77 ~]# cat zzz.txt
123acb111 aaaa bbbb
123acb111 error 11111
this is a test line
aaa bbb ccc
this is a test line
aaaa bbbb cccc
1 111 111
2 222 222
1 111 111
aaa
a 1[34]5 dd
a 135 dd
a 145 dd
loveable aaaa
[root@rhel77 ~]# cat zzz.txt | tr -c a X
XXXaXXXXXXXXaaaaXXXXXXXXXXaXXXXXXXXXXXXXXXXXXXXXXXXXXaXXXXXXXXXXXaaaXXXXXXXXXXXXXXXXXaXXXXXXXXXXXXXaaaaXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXaaaXXXaXXXXXXXXXXXaXXXXXXXXaXXXXXXXXXXXXXXXXaXXXXaaaaX[root@rhel77 ~]#