tr(translate缩写)主要用于删除文件中的控制字符,或进行字符转换。
语法:tr [–c/d/s/t] [SET1] [SET2]
SET1: 字符集1
SET2:字符集2
-c:complement,用SET2替换SET1中没有包含的字符
-d:delete,删除SET1中所有的字符,不转换
-s: squeeze-repeats,压缩SET1中重复的字符
-t: truncate-set1,将SET1用SET2转换,一般缺省为-t
[root@localhost test]# cat test.txt
byte_order little
block_size 8192
data_path data
lob_path lob
charset_name ZHS16GBK
ncharset_name AL32UTF8
output_format text
lob_storage file
clob_byte_order little
trace_level 1
delimiter |
替换小写d至i为大写(-t可省略):
[root@localhost test]# cat test.txt |tr 'd-i' 'D-I'
或
[root@localhost test]# cat test.txt |tr -t 'd-i' 'D-I'
bytE_orDEr lIttlE
block_sIzE 8192
Data_patH Data
lob_patH lob
cHarsEt_namE ZHS16GBK
ncHarsEt_namE AL32UTF8
output_Format tExt
lob_storaGE FIlE
clob_bytE_orDEr lIttlE
tracE_lEvEl 1
DElImItEr |
删除字符‘e':
[root@localhost test]# cat test.txt |tr -d 'e'
byt_ordr littl
block_siz 8192
data_path data
lob_path lob
charst_nam ZHS16GBK
ncharst_nam AL32UTF8
output_format txt
lob_storag fil
clob_byt_ordr littl
trac_lvl 1
dlimitr |
过滤:只显示定义的字母
[root@localhost test]# sed -n '2p' test.txt
block_size 8192
[root@localhost test]# sed -n '2p' test.txt |tr -d -c 'a-z \n'
blocksize
[root@localhost test]#
[root@localhost test]# sed -n '5,6p' test.txt |tr -cs "[a-z][A-Z]" "\n"
charset
name
ZHS
GBK
ncharset
name
AL
UTF
[root@localhost test]# sed -n '5,6p' test.txt
charset_name ZHS16GBK
ncharset_name AL32UTF8
[root@localhost test]#
去重:
[root@localhost test]# sed -n '9p' test.txt | tr -s [a-z]
clob_byte_order litle
[root@localhost test]# sed -n '9p' test.txt | tr -s 'a-z'
clob_byte_order litle
[root@localhost test]#
[root@localhost test]# sed -n '9p' test.txt
clob_byte_order little
[root@localhost test]#
去重:压缩掉空格:
[root@localhost test]# sed -n '4p' test.txt
lob_path lob
[root@localhost test]# sed -n '4p' test.txt |tr -s ' '
lob_path lob