tr命令
阿新 • • 發佈:2018-04-24
tr1.tr命令
tr命令不能從文件中讀取數據,只能從標準輸出中獲取數據,結果寫到輸出設備。
tr命令可以批量轉換或刪除指定的字符。
2.tr命令的使用方法
語法:
tr [選項] 字符集1 字符集2
選項:
-d:刪除指定的字符集1的東西
-s:壓縮字符集1中字符。一排重復的字符會壓縮成一個
-t:字符集2 替換 字符集1,不加選項同結果。
-c:保留字符集1的字符,其他的字符用字符集2替換。
字符集:
[a-z][A-Z][0-9]:很好理解的範圍。
[xx*n]:表示xx出現n次
和所有元字符
3.實例
3.1去字符的重復 -s
s選項可以壓縮文本中連續出現的字符只保留一個。
[root@ams d6]# echo "hhhhi,woooorllld" > 2.txt
[root@ams d6]# tr -s [a-z] < 2.txt
hi,world
去除空行
[root@ams d6]# echo -e "aa\n\n\n\n\naa" > 2.txt
[root@ams d6]# tr -s "\n" < 2.txt
aa
aa
3.2 大小寫轉換 -t
用第二個字符集 替換 第一個字符集。
[root@ams d6]# ls | tr 'a-z' 'A-Z'
1.TXT
2.TXT
3.3 刪除指定字符 -d
單個字符直接引用起來就可以,多個字符需要用中括號括起來。
[root@ams d6]# date | tr -d "[0-9] [:]"
WedSepCST
tr命令