linux命令之tr
1.tr介绍
linux命令tr用于转换或删除文件中的字符。tr命令只能对标准输入进行操作,不能直接对文件进行操作
2.tr用法
tr [参数] [标准输入]
tr参数常规说明
参数 说明
-s 将多个连续的字符压缩为一个字符
-d 删除指定的字符
-t 对指定的字符进行替换
-c 将指定字符外的字符替换为指定字符
3.实例
3.1.将任一连续的a、b、c、d字符压缩为一个字符
命令:
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 ~]#
3.2.将多个连续的a压缩为A
命令:
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 ~]#
3.3.将多个连续的a、连续的b分别压缩为A和B
命令:
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 ~]#
3.4.删除空白行
命令:
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 ~]#
3.5.删除字符a
命令:
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 ~]#
3.6.删除字符a和b
命令:
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 ~]#
3.7.删除字符a、b和c
命令:
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 ~]#
3.8.把字符a、b、c替换为A、B、C
命令:
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 ~]#
3.9.把a-z转换为A-Z
命令:
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 ~]#
3.10.将字符a外的字符全部替换为X
命令:
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
————————————————
版权声明:本文为CSDN博主「小黑要上天」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/z19861216/article/details/132577197