日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

tr用法

發(fā)布時(shí)間:2025/4/5 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tr用法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?tr用來從標(biāo)準(zhǔn)輸入中通過替換或刪除操作進(jìn)行字符轉(zhuǎn)換。 tr主要用于刪除文件中控制字符或進(jìn)行字符轉(zhuǎn)換。

特別要注意一點(diǎn):tr 只能進(jìn)行字符的替換、縮減和刪除,不能用來替換字符串。
最常用選項(xiàng)的tr命令格式為:?
tr -c -d -s ["string1_to_translate_from"] ["string2_to_translate_to"] file?
這里:?
-c 用字符串1中字符集的補(bǔ)集替換此字符集,要求字符集為ASCII。?
-d 刪除字符串1中所有輸入字符。?
-s 刪除所有重復(fù)出現(xiàn)字符序列,只保留第一個(gè);即將重復(fù)出現(xiàn)字符串壓縮為一個(gè)字符串。?
file是轉(zhuǎn)換文件名。雖然可以使用其他格式輸入,但這種格式最常用。

字符范圍?
指定字符串1或字符串2的內(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的字符串。?
tr中特定控制字符的不同表達(dá)方式?
速記符含義八進(jìn)制方式?
\a Ctrl-G? 鈴聲\007?
\b Ctrl-H? 退格符\010?
\f Ctrl-L? 走行換頁\014?
\n Ctrl-J? 新行\(zhòng)012?
\r Ctrl-M? 回車\015?
\t Ctrl-I? tab鍵\011?
\v Ctrl-X? \030?
應(yīng)用例子
(1)去除oops.txt里面的重復(fù)的小寫字符?(?#? -s會(huì)保留第一個(gè)字符)
?????????[root@localhost ~]# cat oops.txt
???????? ddddfffabccccc
???????? lerrrrdddd
???????? [root@localhost ~]#?tr -s "[a-z]" < oops.txt > result.txt
???????? [root@localhost ~]# cat result.txt
???????? dfabc
???????? lerd
?
(2)刪除空行?
???????[root@localhost ~]# cat oops.txt
?????? ddddfffabccccc

?


?????? lerrrrdddd
?????? [root@localhost ~]#??tr -s "[\012]" < oops.txt > result.txt
?????? [root@localhost ~]# cat result.txt
?????? ddddfffabccccc
?????? lerrrrdddd

(3)刪除所有空行
??????? [root@localhost ~]# cat oops.txt
??????? ddddfffabccccc

?


??????? lerrrrdddd
??????? [root@localhost ~]#?tr -d "[\012]" < oops.txt > result.txt
??????? [root@localhost ~]# cat result.txt
??????? ddddfffabccccclerrrrdddd

(4)小寫到大寫?
????????[root@localhost ~]# cat oops.txt
??????? ddddfffabccccc
??????? errrrdddd
??????? [root@localhost ~]#?cat oops.txt | tr "[a-z]" "[A-Z]" > result.txt
??????? [root@localhost ~]# cat result.txt
??????? DDDDFFFABCCCCC
??????? ERRRRDDDD


(5)刪除指定的字符(# -d 與 -s 不同,-d會(huì)全部刪除,但-s會(huì)保留第一個(gè))
???????? [root@localhost ~]# cat oops.txt
???????? ddddfffabccccc
???????? errrrdddd
???????? [root@localhost ~]#?cat oops.txt | tr -d "[bd]" > result.txt
???????? [root@localhost ~]# cat result.txt
???????? fffaccccc
???????? errrr
???????? [root@localhost ~]#?cat oops.txt | tr -s "[bd]" > result.txt
???????? [root@localhost ~]# cat result.txt
???????? dfffabccccc
???????? errrrd

(6)替代指定的字符(#一對(duì)一的替代)
?????????[root@localhost ~]# cat oops.txt
???????? ddddfffabccccc
???????? errrrdddd
???????? [root@localhost ~]#?cat oops.txt | tr "[bd]" "[BD]" > result.txt
???????? [root@localhost ~]# cat result.txt
???????? DDDDfffaBccccc
???????? errrrDDDD

轉(zhuǎn)載于:https://blog.51cto.com/6272407/1165236

總結(jié)

以上是生活随笔為你收集整理的tr用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。