linux tr命令對來自標準輸入的字符進行轉換、刪除及壓縮

tr命令對來自標準輸入的字符進行轉換、刪除及壓縮,是個轉換命令
tr [options] set1 set2 就是把set1集合中的內容換成set2集合中的內容
1將大寫字母變爲小寫字母
[root@localhost test]# echo "HELLO LUFUBO NICE TO MEET YOU" |tr 'A-Z' 'a-z'
hello lufubo nice to meet you

2將數字加密與解密:集合映射    
[root@localhost test]# echo 12345 | tr '0-9' '987654321'
87654

[root@localhost test]# echo 87654 | tr '9876543210' '0-9'
12345


3 -d刪除字符
[root@localhost test]# echo "hello 123 world 456" | tr -d '0-9'
hello  world

4 -c 集合的補集
    從輸入文本中將不在補集中的所有字符全部刪除
[root@localhost test]# echo "hello 123 world 456" | tr -d -c '0-9'
123456

5 -s 壓縮重複字符
[root@localhost test]# echo "hello            lufubo" | tr -s ' '
hello lufubo


6 將文件中的進行相加
[root@localhost test]# cat sum.txt 
1
2
3
4
5
[root@localhost test]# cat sum.txt | echo $[ $(tr '\n' '+') 0 ] #0是爲了最後個‘/n’替換成+後再加個0的意思,這個可以成將文本里面的所有數字相加喲。~^~
15
7 字符類
    tr可以像使用集合一樣使用各種不同的字符類,這些字符如下:
alnum:字母和數字
alpha:字母
cntrl:控制字符
digit:數字
graph:圖形字符
lower:小寫字母
print:可打印字符
punct:標點符號
space:空白字符
upper:大寫字母
xdigit:十六進制字符

語法: tr [:class:] [:class:]
例如
[root@localhost test]# echo lufubo | tr '[:lower:]' '[:upper:]'   
LUFUBO

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章