linux中字符轉(zhuǎn)換命令-tr的用法
可以把tr看作為一個(gè)簡(jiǎn)化的sed工具,tr表示為:translate。tr命令主要用于實(shí)現(xiàn)以下兩個(gè)功能
- 替換操作的字符串轉(zhuǎn)換。
- 刪除操作的字符串轉(zhuǎn)換,可以很容易的刪除一些控制字符或者是空行。
tr命令能夠?qū)崿F(xiàn)的功能,都能夠用sed命令來(lái)實(shí)現(xiàn)。但就具體的替換功能來(lái)說(shuō),tr用起來(lái)更容易,也比較簡(jiǎn)單。
一,命令格式
[html] view plaincopy
- tr [option] ["string1"] ["string2"] < file
- 默認(rèn)選項(xiàng)。就是沒(méi)有任何選項(xiàng)的時(shí)候,tr默認(rèn)為替換操作,就是將string1在文件中出現(xiàn)的字符替換為string2中的字符,這里要注意的是替換關(guān)系。
- -c選項(xiàng),用string1中字符的補(bǔ)集替換string1,這里的字符集為ASCII。
- -d選項(xiàng),刪除文件中所有在string1中出現(xiàn)的字符。
- -s選項(xiàng),刪除文件中重復(fù)并且在string1中出現(xiàn)的字符,只保留一個(gè)。
-c選項(xiàng)在使用時(shí),只是將string1替換為現(xiàn)在的補(bǔ)集,如在使用
[html] view plaincopy
- [root@localhost client]# echo "hello world,root,2012" | tr -c "0-9" "*"
- *****************2012*
如果只需要替換數(shù)字的話:
[html] view plaincopy
- [root@localhost client]# echo "hello world,root,2012" | tr "0-9" "*"
- hello world,root,****
二,字符串的取值范圍
指定string或string2的內(nèi)容時(shí),只能使用單字符或字符串范圍或列表。
- [a-z] a-z內(nèi)的字符組成的字符串。
- [A-Z] A-Z內(nèi)的字符組成的字符串。
- [0-9] 數(shù)字串。
- \octal 一個(gè)三位的八進(jìn)制數(shù),對(duì)應(yīng)有效的ASCII字符。
- [O*n] 表示字符O重復(fù)出現(xiàn)指定次數(shù)n。因此[O*2]匹配OO的字符串。
三,控制字符的不同表達(dá)方式
速記符 | 含義 | 八進(jìn)制方式 |
---|---|---|
\a | Ctrl-G | 鈴聲\007 |
\b | Ctrl-H | 退格符\010 |
\f | Ctrl-L | 走行換頁(yè)\014 |
\n | Ctrl-J | 新行\(zhòng)012 |
\r | Ctrl-M | 回車\015 |
\t | Ctrl-I | tab鍵\011 |
\v | Ctrl-X | \030 |
四,字符替換
這是tr的默認(rèn)操作,先看下面的命令和輸出
[html] view plaincopy
- [root@localhost client]# echo "hello world" | tr "a-z" "A-Z"
- HELLO WORLD
- [root@localhost client]# echo "hello world" | tr "a-l" "A-Z"
- HELLo worLD
- [root@localhost client]# echo "hello world" | tr "a-z" "A-H"
- HEHHH HHHHD
第二行輸出將小寫(xiě)中的a-l分別換成A-L,而將小寫(xiě)中的l以后的字符都不替換。
第三行輸出將小寫(xiě)中的a-h換成A-H,而h以后的字符都換成H,因?yàn)楹笳叩奶鎿Q空間沒(méi)有前面的字符空間大,所以就重復(fù)后面的H,相當(dāng)于后面的字符是A-HHH......HHHHH。
如果我們想要進(jìn)行大小寫(xiě)轉(zhuǎn)換,可以按下面的輸入:
[html] view plaincopy
- tr "a-z" "A-Z" < inputfile
五,去除重復(fù)字符
這個(gè)時(shí)候,所用的選項(xiàng)是-s選項(xiàng),如:
[html] view plaincopy
- [root@localhost client]# echo "hello world,root" | tr -s "ao"
- hello world,rot
- [root@localhost client]# echo "hello world,root" | tr -s "lo"
- helo world,rot
- [root@localhost client]# echo "hello world,root" | tr -s "a-z"
- helo world,rot
- [root@localhost client]# echo "hello world,root" | tr -s "0-9"
- hello world,root
第二行將hello和root兩個(gè)字符都?jí)嚎s了。
第三行表示將a-z中的除復(fù)字符都去掉。
第三行表示將字符串中的重復(fù)的且重復(fù)字符在0-9字符集中的字符去掉,這里沒(méi)有。
如果我們想要去掉空行,可以這樣操作:
[html] view plaincopy
- tr -s "\n" < inputfile 或者 tr -s "\012" <inputfile // 這兩個(gè)是一樣的。
六,刪除字符
-d選項(xiàng)和-s選項(xiàng)類似,只不過(guò)-d選項(xiàng)會(huì)刪除所有出現(xiàn)的字符。
[html] view plaincopy
- [root@localhost client]# echo "hello world,root" | tr -d "a-h"
- llo worl,root
- [root@localhost client]# echo "hello world,root,2012" | tr -d "a-z"
- ,,2012
- [root@localhost client]# echo "hello world,root,2012" | tr -d "0-9"
- hello world,root,
個(gè)人網(wǎng)站: www.software8.co
posted on 2012-09-28 09:27 你爸是李剛 閱讀(134) 評(píng)論(0) 編輯 收藏