tr - translate or delete characters
Translate, squeeze, and/or delete characters from standard input, writing to standard output.
-c, -C, --complement
first complement SET1
-d, --delete
delete characters in SET1, do not translate
-s, --squeeze-repeats
replace each input sequence of a repeated character that is
listed in SET1 with a single occurrence of that character
-t, --truncate-set1
first truncate SET1 to length of SET2
tr 命令的格式:tr 选项 [集合1] [集合2]
-d 参数:删除文件中出现在集合一中的字符。
[useputty_login@PC_IN_LAN ~]$cat a
1aaaaaacc
2bbbbbbdd
[useputty_login@PC_IN_LAN ~]$tr -d [1] < a(删除了1)
aaaaaacc
2bbbbbbdd
-s 参数:压缩文件中在集合1中出现的字符
[useputty_login@PC_IN_LAN ~]$cat a
1aaaaaacc
2bbbbbbdd
[useputty_login@PC_IN_LAN ~]$tr -s [a] < a
1acc
2bbbbbbdd
用-s 选项删除空行
[useputty_login@PC_IN_LAN ~]$cat -A b
bbb$
$
$
ddd$
$
$
fff$
$
$
hhh$
[useputty_login@PC_IN_LAN ~]$tr -s "\n" < b
bbb
ddd
fff
hhh
首先我将文件理解成这样:
bbb\n\n\n
ddd\n\n\n
fff\n\n\n
hhh\n
因为-s 选项有压缩重复字符的作用,这里将\n压缩成每行一个,所以相当于去除空行。
-s 可以删除某些控制字符^M 思路是将结尾的控制字符替换为\n然后压缩
[useputty_login@PC_IN_LAN ~]$cat -A a
1aaaaaacc^M$
2bbbbbbdd^M$
[useputty_login@PC_IN_LAN ~]$tr -s "^M" "\n" < a
1aaaaaacc
2bbbbbbdd
[useputty_login@PC_IN_LAN ~]$tr -s "^M" "\n" < a > b
[useputty_login@PC_IN_LAN ~]$cat -A b
1aaaaaacc$
2bbbbbbdd$
-cs (-c -s)将集合1之外的替换成集合2中的字符,然后squeeze!
-t 参数:将文件中出现在集合1中的字符转换成集合2中出现得字符 一一对应转换。
[useputty_login@PC_IN_LAN ~]$cat a
1aaaaaacc
2bbbbbbdd
[useputty_login@PC_IN_LAN ~]$tr "a" "e" < a
1eeeeeecc
2bbbbbbdd
-c 就表示是集合1的补集
[useputty_login@PC_IN_LAN ~]$cat a
1aaaaaacc
2bbbbbbdd
[useputty_login@PC_IN_LAN ~]$tr -c "a\n" "e" < a (这里就是把文件中除a和换行符之外的所有字符都替换成e,应为每行文件末尾都有个换行符)
eaaaaaaee
eeeeeeeee
阅读(1566) | 评论(0) | 转发(0) |