Linux常用基本命令:tr-替換或者刪除字符


tr命令

作用:從標准輸入中替換,縮減或者刪除字符,並將結果輸出到標准輸出

格式:tr [option] [set1] [set2]

tr [選項] [字符1] [字符2]

把y替換m, o替換e,並不僅僅是yo替換me

ghostwu@dev:~/linux/tr$ cat ghostwu.txt 
hello,my name is ghostwu,
my qq is 359173352@qq.com
my blog is http://www.cnblogs.com/ghostwu
nice to meet you,
feel free to contact me.
ghostwu@dev:~/linux/tr$ tr 'me' 'yo' < ghostwu.txt 
hollo,yy nayo is ghostwu,
yy qq is 359173352@qq.coy
yy blog is http://www.cnblogs.coy/ghostwu
nico to yoot you,
fool froo to contact yo.

小寫字母變成大寫

ghostwu@dev:~/linux/tr$ tr '[a-z]' '[A-Z]' < ghostwu.txt 
HELLO,MY NAME IS GHOSTWU,
MY QQ IS 359173352@QQ.COM
MY BLOG IS HTTP://WWW.CNBLOGS.COM/GHOSTWU
NICE TO MEET YOU,
FEEL FREE TO CONTACT ME.

-d: 刪除.     刪除數字

ghostwu@dev:~/linux/tr$ tr -d '[0-9]' < ghostwu.txt 
hello,my name is ghostwu,
my qq is @qq.com
my blog is http://www.cnblogs.com/ghostwu
nice to meet you,
feel free to contact me.

刪除a, b, c中出現的任意一個字母

ghostwu@dev:~/linux/tr$ tr -d ['a-c'] < ghostwu.txt 
hello,my nme is ghostwu,
my qq is 359173352@qq.om
my log is http://www.nlogs.om/ghostwu
nie to meet you,
feel free to ontt me.

刪除文件中的換行符

ghostwu@dev:~/linux/tr$ tr -d '\n' < ghostwu.txt 
hello,my name is ghostwu,my qq is 359173352@qq.commy blog is http://www.cnblogs.com/ghostwunice to meet you,feel free to contact me.

-s:保留連續字符的第一個,其他的刪除。 壓縮連續字符

ghostwu@dev:~/linux/tr$ echo 'gggghhostwwwu' | tr -s ghostwu
ghostwu

-c:取反。 把所有的非數字 都變成 #

ghostwu@dev:~/linux/tr$ tr '[0-9]' '#' < ghostwu.txt 
hello,my name is ghostwu,
my qq is #########@qq.com
my blog is http://www.cnblogs.com/ghostwu
nice to meet you,
feel free to contact me.
ghostwu@dev:~/linux/tr$ tr -c '[0-9]' '#' < ghostwu.txt 
###################################359173352#############################################################################################ghostwu@dev:~/linux/tr$

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM