什麼是tr命令?tr,translate的簡寫,translate的翻譯:linux
[trænsˈleit]git
vi. 翻譯, 能被譯出算法
vt. 翻譯, 解釋, 轉化, 轉變爲, 調動bash
在這裏用到的意思是轉化,轉變,轉換,在linux下輸入tr --help查看一下提示:app
amosli@amosli-pc:~$ tr --help Usage: tr [OPTION]... SET1 [SET2] Translate, squeeze, and/or delete characters from standard input, writing to standard output. -c, -C, --complement use the complement of SET1 -d, --delete delete characters in SET1, do not translate -s, --squeeze-repeats replace each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character -t, --truncate-set1 first truncate SET1 to length of SET2 --help display this help and exit --version output version information and exit SETs are specified as strings of characters. Most represent themselves. Interpreted sequences are: \NNN character with octal value NNN (1 to 3 octal digits) \\ backslash \a audible BEL \b backspace \f form feed \n new line \r return \t horizontal tab \v vertical tab CHAR1-CHAR2 all characters from CHAR1 to CHAR2 in ascending order [CHAR*] in SET2, copies of CHAR until length of SET1 [CHAR*REPEAT] REPEAT copies of CHAR, REPEAT octal if starting with 0 [:alnum:] all letters and digits [:alpha:] all letters [:blank:] all horizontal whitespace [:cntrl:] all control characters [:digit:] all digits [:graph:] all printable characters, not including space [:lower:] all lower case letters [:print:] all printable characters, including space [:punct:] all punctuation characters [:space:] all horizontal or vertical whitespace [:upper:] all upper case letters [:xdigit:] all hexadecimal digits [=CHAR=] all characters which are equivalent to CHAR Translation occurs if -d is not given and both SET1 and SET2 appear. -t may be used only when translating. SET2 is extended to length of SET1 by repeating its last character as necessary. Excess characters of SET2 are ignored. Only [:lower:] and [:upper:] are guaranteed to expand in ascending order; used in SET2 while translating, they may only be used in pairs to specify case conversion. -s uses SET1 if not translating nor deleting; else squeezing uses SET2 and occurs after translation or deletion.
全是英文?翻譯過來看下:ui
tr [選項]… 集合1 [集合2] 選項說明: -c, -C, –complement 用集合1中的字符串替換,要求字符集爲ASCII。 -d, –delete 刪除集合1中的字符而不是轉換 -s, –squeeze-repeats 刪除全部重複出現字符序列,只保留第一個;即將重複出現字符串壓縮爲一個字符串。 -t, –truncate-set1 先刪除第一字符集較第二字符集多出的字符 字符集合的範圍: \NNN 八進制值的字符 NNN (1 to 3 爲八進制值的字符) \\ 反斜槓 \a Ctrl-G 鈴聲 \b Ctrl-H 退格符 \f Ctrl-L 走行換頁 \n Ctrl-J 新行 \r Ctrl-M 回車 \t Ctrl-I tab鍵 \v Ctrl-X 水平製表符 CHAR1-CHAR2 從CHAR1 到 CHAR2的全部字符按照ASCII字符的順序 [CHAR*] in SET2, copies of CHAR until length of SET1 [CHAR*REPEAT] REPEAT copies of CHAR, REPEAT octal if starting with 0 [:alnum:] 全部的字母和數字 [:alpha:] 全部字母 [:blank:] 水平製表符,空白等 [:cntrl:] 全部控制字符 [:digit:] 全部的數字 [:graph:] 全部可打印字符,不包括空格 [:lower:] 全部的小寫字符 [:print:] 全部可打印字符,包括空格 [:punct:] 全部的標點字符 [:space:] 全部的橫向或縱向的空白 [:upper:] 全部大寫字母
tr用來從標準輸入中經過替換或刪除操做進行字符轉換。tr主要用於刪除文件中控制字符或進行字符轉換。使用tr時要轉換兩個字符串:字符串1用於查詢,字符串2用於處理各類轉換。tr剛執行時,字符串1中的字符被映射到字符串2中的字符,而後轉換操做開始。this
經過使用 tr,您能夠很是容易地實現 sed 的許多最基本功能。您能夠將 tr 看做爲 sed 的(極其)簡化的變體:它能夠用一個字符來替換另外一個字符,或者能夠徹底除去一些字符。您也能夠用它來除去重複字符。這就是全部 tr 所可以作的。加密
tr -c -d -s ["string1_to_translate_from"] ["string2_to_translate_to"] < input-file
這裏:spa
指定字符串1或字符串2的內容時,只能使用單字符或字符串範圍或列表。
[a-z] a-z內的字符組成的字符串。
[A-Z] A-Z內的字符組成的字符串。
[0-9] 數字串。
\octal 一個三位的八進制數,對應有效的ASCII字符。
[O*n] 表示字符O重複出現指定次數n。所以[O*2]匹配OO的字符串。翻譯
速記符含義八進制方式 \a Ctrl-G 鈴聲\007 \b Ctrl-H 退格符\010 \f Ctrl-L 走行換頁\014 \n Ctrl-J 新行\012 \r Ctrl-M 回車\015 \t Ctrl-I tab鍵\011 \v Ctrl-X \030
[root@Gin scripts]# cat t.txt abc [root@Gin scripts]# cat t.txt |tr "abc" "xyz" xyz [root@Gin scripts]# cat t.txt abc
[root@Gin scripts]# cat file abc [root@Gin scripts]# cat file|tr [a-z] [A-Z] ABC
大寫轉小寫只須要把tr後面的參數換個位置便可!
[root@Gin scripts]# cat file|tr [0-9] [a-j] abcdefghij
[root@Gin scripts]# cat file what is Snail [root@Gin scripts]# cat file|tr -d "Snail" wht s [root@Gin scripts]# cat file what is Snail
# cat file | tr -d "\n\t"
# cat file | tr -s "\n" > new_file
# cat file | tr -d "\r" 或者 # cat file | tr -s "\r" "\n"
# cat file | tr -s "\011" "\040"
# echo $PATH | tr -s ":" "\n"
[root@Gin scripts]# echo 12345|tr '0-9' '987654321' ## 加密 87654 [root@Gin scripts]# echo 87654|tr '987654321' '0-9' ## 解密 12345
上面是一個很是有趣的小例子,經過映射來實現簡單的加密解密,看懂這個例子,能夠接着往下看古羅馬時期發明的凱撒加密的一種變體ROT13
[root@Gin scripts]# echo "hi,this is amosli" | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm' uv,guvf vf nzbfyv [root@Gin scripts]# echo "uv,guvf vf nzbfyv" | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm' hi,this is amosli
ROT13是它本身自己的逆反;也就是說,要還原ROT13,套用加密一樣的算法便可得,故一樣的操做可用再加密與解密。很是神奇!
tr -c [set1] [set2]
set1的補集意味着從這個集合中包含set1中沒有的全部字符。最典型的用法就是從輸入文本中將不在補集中的全部字符所有刪除。例如:
[root@Gin scripts]# echo "hello 123 world " | tr -d -c '0-9 \n' 123
在這裏,補集中包含了除數字、空格字符和換行符以外的全部字符,由於指定了-d,因此這些字符所有都會被刪除。
[root@Gin scripts]# echo "GNU is not UNIX . Recursicve right?" | tr -s ' ' GNU is not UNIX . Recursicve right?
使用-s參數能夠壓縮字符串中重複的字符。看另外一個例子:
[root@Gin scripts]# cat sum.txt 5 4 3 5 4 3 [root@Gin scripts]# cat sum.txt|echo $[ $(tr '\n' '+') 0 ] 24 [root@Gin scripts]# cat sum.txt|echo $[ $(tr '\n' '+') ] -bash: 5+4+3+5+4+3+ : syntax error: operand expected (error token is "+ ")
這裏,運用tr實現了加法運算, tr '\n' '+'使用換行符來替換爲'+'而後鏈接起來,最後多出來一個'+'再接上數字0即實現了加法。