一、文本處理命令
1.1 sort命令
Linux sort命令用于將文本檔案內容加以排序,
sort 可針對文本檔案的內容,以行為單位來排序,
語法格式如下:
sort [引數]...[檔案]
| 相關引數 | 引數說明 |
|---|---|
| -n | 依照數值的大小排序 |
| -r | 以相反的順序來排序 |
| -k | 以某列進行排序 |
| -t | 指定分割符,默認是以空格為分隔符 |
| … |
案例:
$ cat testfile # testfile檔案原有排序
test 30
Hello 95
Linux 85
使用sort命令重新排序后的結果如如下:
$ sort testfile # 重排結果
Hello 95
Linux 85
test 30
使用 -k 引數設定對第二列的值進行重排,結果如下:
$ sort testfile -k 2
test 30
Linux 85
Hello 95
1.2 uniq命令
Linux uniq 命令用于檢查及洗掉文本檔案中重復出現的行列,一般與 sort 命令結合使用,
uniq 可檢查文本檔案中重復出現的行列,
語法格式如下:
uniq [-cdu]...[輸入檔案][輸出檔案]
| 相關引數 | 引數說明 |
|---|---|
| -c | 在每列旁邊顯示該行重復出現的次數 |
| -d | 僅顯示重復出現的行列 |
| -u | 僅顯示出一次的行列 |
| [輸入檔案] | 指定已排序好的文本檔案,如果不指定此項,則從標準讀取資料 |
| [輸出檔案] | 指定輸出的檔案,如果不指定此選項,則將內容顯示到標準輸出設備(顯示終端) |
| … |
案例:
testfile中的原有內容為:
$ cat testfile #原有內容
test 30
test 30
test 30
Hello 95
Hello 95
Hello 95
Hello 95
Linux 85
Linux 85
使用uniq 命令洗掉重復的行后,有如下輸出結果:
$ uniq testfile #洗掉重復行后的內容
test 30
Hello 95
Linux 85
$ uniq -c testfile #洗掉重復行后的內容
3 test 30 #前面的數字的意義為該行共出現了3次
4 Hello 95 #前面的數字的意義為該行共出現了4次
2 Linux 85 #前面的數字的意義為該行共出現了2次
當重復的行并不相鄰時,uniq 命令是不起作用的,即若檔案內容為以下時,uniq 命令不起作用:
$ cat testfile1 # 原有內容
test 30
Hello 95
Linux 85
test 30
Hello 95
Linux 85
test 30
Hello 95
Linux 85
這時我們就可以使用 sort:
$ sort testfile1 | uniq
Hello 95
Linux 85
test 30
統計各行在檔案中出現的次數:
$ sort testfile1 | uniq -c
3 Hello 95
3 Linux 85
3 test 30
在檔案中找出重復的行:
$ sort testfile1 | uniq -d
Hello 95
Linux 85
test 30
1.3 cut命令
Linux cut命令用于顯示每行從開頭算起 num1 到 num2 的文字,
語法格式如下:
cut [-c] [file]
cut [-df] [file]
...
使用說明:
cut 命令從檔案的每一行剪切位元組、字符和欄位并將這些位元組、字符和欄位寫至標準輸出,
如果不指定 File 引數,cut 命令將讀取標準輸入,必須指定 -b、-c 或 -f 標志之一,
| 相關引數 | 引數說明 |
|---|---|
| -c | 以字符為單位進行分割 |
| -d | 自定義分隔符,默認為制表符 |
| -f | 與-d一起使用,指定顯示哪個區域 |
| … |
案例:
[root@jiangzhi ~]# cat juechen # 原檔案內容
|7|9|8|7|5|5|3|4|5|1|3|4|5
|7|8|7|8|5|2|5|4|5|1
|6|5|7|6|7|6|5|3|2
|6|5|6|7|6|5|7
|3|2|4|3|4|5|2|3|4|6|2|5
|2|3|3|5|3|4|6
|2|1|5|3|4|5|3|2|4
|5|4|5|4|3
|2|5|3|4|5|2|3
如果我們想提取每一行的第2個位元組,可以這樣:
[root@jiangzhi ~]# cat juechen | cut -b2
7
7
6
6
3
2
2
5
2
1.4 tr命令
Linux tr 命令用于轉換或洗掉檔案中的字符,
tr 指令從標準輸入設備讀取資料,經過字串轉譯后,將結果輸出到標準輸出設備,
語法格式如下:
tr [-cdst]...[第一字符集][第二字符集]
tr [OPTION]…SET1[SET2]
| 相關引數 | 引數說明 |
|---|---|
| -c, --complement | 反選設定字符,也就是符合 SET1 的部份不做處理,不符合的剩余部份才進行轉換 |
| -d, --delete | 洗掉指令字符 |
| -s, --squeeze-repeats | 縮減連續重復的字符成指定的單個字符 |
| -t, --truncate-set1 | 削減 SET1 指定范圍,使之與 SET2 設定長度相等 |
| … |
案例:
testfile檔案中的內容如下:
$ cat testfile #testfile原來的內容
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.
將檔案testfile中的小寫字母全部轉換成大寫字母,此時,可使用如下命令:
cat testfile |tr a-z A-Z
使用 tr 命令大小寫轉換后,得到如下輸出結果:
$ cat testfile | tr a-z A-Z #轉換后的輸出
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.
1.5 wc命令
Linux wc命令用于計算字數,
利用wc指令我們可以計算檔案的Byte數、字數、或是行數,若不指定檔案名稱、或是所給予的檔案名為"-",則wc指令會從標準輸入設備讀取資料,
語法格式如下:
wc [-clw]...[檔案...]
| 相關引數 | 引數說明 |
|---|---|
| -c | 統計檔案的Bytes數 |
| -l | 統計檔案的行數 |
| -w | 統計檔案中單詞的個數,默認以空白字符做為分隔符 |
注:在Linux系統中,一段連續的數字或字母組合為一個詞
實體:
在默認的情況下,wc將計算指定檔案的行數、字數,以及位元組數,先查看testfile檔案的內容,可以看到:
$ cat testfile
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.
使用wc統計,結果如下:
$ wc testfile # testfile檔案的統計資訊
3 91 601 testfile # testfile檔案的行數為3、單詞數91、位元組數601
其中,3 個數字分別表示testfile檔案的行數、單詞數,以及該檔案的位元組數,
如果想同時統計多個檔案的資訊,例如同時統計testfile、testfile_1、testfile_2,可使用如下命令:
wc testfile testfile_1 testfile_2 #統計三個檔案的資訊
輸出結果如下:
$ wc testfile testfile_1 testfile_2 #統計三個檔案的資訊
3 91 601 testfile # testfile檔案的行數為3、單詞數91、位元組數601
9 18 78 testfile_1 #第二個檔案的行數為9、單詞數18、位元組數78
3 6 32 testfile_2 #第三個檔案的行數為3、單詞數6、位元組數32
15 115 711 總用量 #三個檔案總共的行數為15、單詞數115、位元組數711
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/388898.html
標籤:其他
上一篇:Windows Docker 固定容器IP地址 透明網路驅動程式
下一篇:Linux三劍客命令—sed
