linux的tr命令
linux的tr命令
tr命令在linux系統(tǒng)下主要是用于轉(zhuǎn)換或刪除文件中的字符。下面由學習啦小編為大家整理了linux的tr命令的相關(guān)知識,希望大家喜歡!
linux的tr命令詳解
Linux的tr命令用于轉(zhuǎn)換或刪除文件中的字符。
tr 指令從標準輸入設(shè)備讀取數(shù)據(jù),經(jīng)過字符串轉(zhuǎn)譯后,將結(jié)果輸出到標準輸出設(shè)備。
語法
tr [-cdst][--help][--version][第一字符集][第二字符集]
tr [OPTION]…SET1[SET2]
參數(shù)說明:
-c, --complement:反選設(shè)定字符。也就是符合 SET1 的部份不做處理,不符合的剩余部份才進行轉(zhuǎn)換
-d, --delete:刪除指令字符
-s, --squeeze-repeats:縮減連續(xù)重復的字符成指定的單個字符
-t, --truncate-set1:削減 SET1 指定范圍,使之與 SET2 設(shè)定長度相等
--help:顯示程序用法信息
--version:顯示程序本身的版本信息
字符集合的范圍:
\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 碼的次序為基礎(chǔ),只能由小到大,不能由大到小。
[CHAR*] :這是 SET2 專用的設(shè)定,功能是重復指定的字符到與 SET1 相同長度為止
[CHAR*REPEAT] :這也是 SET2 專用的設(shè)定,功能是重復指定的字符到設(shè)定的 REPEAT 次數(shù)為止(REPEAT 的數(shù)字采 8 進位制計算,以 0 為開始)
[:alnum:] :所有字母字符與數(shù)字
[:alpha:] :所有字母字符
[:blank:] :所有水平空格
[:cntrl:] :所有控制字符
[:digit:] :所有數(shù)字
[:graph:] :所有可打印的字符(不包含空格符)
[:lower:] :所有小寫字母
[:print:] :所有可打印的字符(包含空格符)
[:punct:] :所有標點字符
[:space:] :所有水平與垂直空格符
[:upper:] :所有大寫字母
[:xdigit:] :所有 16 進位制的數(shù)字
[=CHAR=] :所有符合指定的字符(等號里的 CHAR,代表你可自訂的字符)
linux的tr命令實例
將文件testfile中的小寫字母全部轉(zhuǎn)換成大寫字母,此時,可使用如下命令:
cat testfile |tr a-z A-Z
testfile文件中的內(nèi)容如下:
$ cat testfile #testfile原來的內(nèi)容
Linux networks are becoming more and more common,
but scurity is often an overlooked
issue. Unfortunately, in today’s environment all networks
are potential hacker targets,
fro0m tp-secret military research networks to small home LANs.
Linux Network Securty focuses on securing Linux in a
networked environment, where the
security of the entire network needs to be considered
rather than just isolated machines.
It uses a mix of theory and practicl techniques to
teach administrators how to install and
use security applications, as well as how the
applcations work and why they are necesary.
使用 tr 命令大小寫轉(zhuǎn)換后,得到如下輸出結(jié)果:
$ cat testfile | tr a-z A-Z #轉(zhuǎn)換后的輸出
LINUX NETWORKS ARE BECOMING MORE AND MORE COMMON, BUT SCURITY IS OFTEN AN OVERLOOKED
ISSUE. UNFORTUNATELY, IN TODAY’S ENVIRONMENT ALL NETWORKS ARE POTENTIAL HACKER TARGETS,
FROM TP-SECRET MILITARY RESEARCH NETWORKS TO SMALL HOME LANS.
LINUX NETWORK SECURTY FOCUSES ON SECURING LINUX IN A NETWORKED ENVIRONMENT, WHERE THE
SECURITY OF THE ENTIRE NETWORK NEEDS TO BE CONSIDERED RATHER THAN JUST ISOLATED MACHINES.
IT USES A MIX OF THEORY AND PRACTICL TECHNIQUES TO TEACH ADMINISTRATORS HOW TO INSTALL AND
USE SECURITY APPLICATIONS, AS WELL AS HOW THE APPLCATIONS WORK AND WHY THEY ARE NECESARY.
大小寫轉(zhuǎn)換,也可以通過[:lower][:upper]參數(shù)來實現(xiàn)。例如使用如下命令:
cat testfile |tr [:lower:] [:upper:]
輸出結(jié)果如下:
$ cat testfile | tr [:lower:] [:upper:] #轉(zhuǎn)換后的輸出
LINUX NETWORKS ARE BECOMING MORE AND MORE COMMON, BUT SCURITY IS OFTEN AN OVERLOOKED
ISSUE. UNFORTUNATELY, IN TODAY’S ENVIRONMENT ALL NETWORKS ARE POTENTIAL HACKER TARGETS,
FROM TP-SECRET MILITARY RESEARCH NETWORKS TO SMALL HOME LANS.
LINUX NETWORK SECURTY FOCUSES ON SECURING LINUX IN A NETWORKED ENVIRONMENT, WHERE THE
SECURITY OF THE ENTIRE NETWORK NEEDS TO BE CONSIDERED RATHER THAN JUST ISOLATED MACHINES.
IT USES A MIX OF THEORY AND PRACTICL TECHNIQUES TO TEACH ADMINISTRATORS HOW TO INSTALL AND
USE SECURITY APPLICATIONS, AS WELL AS HOW THE APPLCATIONS WORK AND WHY THEY ARE NECESARY.