Linux系統學習之字元處理
管道
管道是一種使用非常頻繁的通訊機制,我們可以使用管道符"|"來連線程式,
由管道連線起來訂單程式可以自動執行,如同有一個資料流一樣,所以管道表現為輸入輸出重定向的一種方法,
它可以把一個命令的輸出內容當作下一個命令的輸入內容,兩個命令之間只需要管道符連線即可
使用grep搜尋文字
grep [-ivnc] `需要匹配的字元` 檔名
#-i 不區分大小寫
#-c 統計包含匹配的行數
#-n 輸出行號
#-v 反向匹配
例子:
[root@Cfhost-170820-UCNK ~]# cat test.txt
The cat`s name is Tom,What`s the mouse`s name?
The mouse`s NAME is Jerry
They are good friends
[root@Cfhost-170820-UCNK ~]# grep `name` test.txt //搜尋含有`name`的句子
The cat`s name is Tom,What`s the mouse`s name?
[root@Cfhost-170820-UCNK ~]# grep -i `name` test.txt //搜尋含有`name`的句子,忽略大小寫
The cat`s name is Tom,What`s the mouse`s name?
The mouse`s NAME is Jerry
[root@Cfhost-170820-UCNK ~]# grep -c `name` test.txt//含`name`的句子有多少條
1
[root@Cfhost-170820-UCNK ~]# grep -ci `name` test.txt//含`name‘的句子有多少條,大小寫可忽略
2
[root@Cfhost-170820-UCNK ~]# grep -v `name` test.txt //搜尋不含`name`的句子
The mouse`s NAME is Jerry
They are good friends
[root@Cfhost-170820-UCNK ~]# grep -vi `name` test.txt//搜尋不含`name`的句子,大寫的NAME也過濾掉
They are good friends
[root@Cfhost-170820-UCNK ~]# cat test.txt
The cat`s name is Tom,What`s the mouse`s name?
The mouse`s NAME is Jerry
They are good friends
[root@Cfhost-170820-UCNK ~]# cat test.txt | grep -vi `name` ? //以上命令都可以使用管道符改寫,比如上一個命令可以這樣寫,意思都是一樣的
They are good friends
使用sort排序
sort [-ntkr] 檔名
#-n 採取數字排序
#-t 指定分隔符
#-k 指定第幾列
#-r 反向排序
[root@Cfhost-170820-UCNK ~]# cat sort.txt
b:3
c:2
a:4
e:5
d:1
f:11
[root@Cfhost-170820-UCNK ~]# cat sort.txt | sort //按字母正向排序
a:4
b:3
c:2
d:1
e:5
f:11
[root@Cfhost-170820-UCNK ~]# cat sort.txt | sort -r //按字母反向排序
f:11
e:5
d:1
c:2
b:3
a:4
[root@Cfhost-170820-UCNK ~]# cat sort.txt | sort -t ":" -k 2 -n
d:1
c:2
b:3
a:4
e:5
f:11
使用uniq刪除重複內容
uniq [-ic] #-i 忽略大小寫 #-c 計算重複內容 [root@Cfhost-170820-UCNK ~]# cat uniq.txt | sort | uniq //需要說明的是uniq命令一般需要和sort一起使用,也就是先將檔案使用進行sort排序,然後再使用uniq刪除重複的內容。單獨加上uniq不加sort是沒有效果的。 123 abc 補充說明: [root@Cfhost-170820-UCNK ~]# cat uniq.txt | uniq abc 123 abc 123 [root@Cfhost-170820-UCNK ~]# cat uniq.txt | sort | uniq -c //使用-c引數就會在每行前面列印出改行重複的次數 2 123
使用cutt擷取文字、使用tr做文字轉換、使用paste做文字合併(還有一個檔案分割用split做,這裡不再說了,我目前想不到它到底有什麼用)
cut -f 指定列 -d `分隔符`
[root@Cfhost-170820-UCNK ~]# cat /etc/passwd | cut -f1 -d `:`
root
bin
daemon
admin
tr命令比較簡單,其主要作用在於文字轉換或刪除
[root@Cfhost-170820-UCNK ~]# cat /etc/passwd | tr `[a-z]` `[A-Z]`
ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH
BIN:X:1:1:BIN:/BIN:/SBIN/NOLOGIN
paste的作用在於將檔案按照行進行合併,中間使用tab隔開
[root@Cfhost-170820-UCNK ~]# cat a.txt
a b c
[root@Cfhost-170820-UCNK ~]# cat b.txt
a b c
[root@Cfhost-170820-UCNK ~]# paste a.txt b.txt
a b c a b c
相關文章
- Linux系統中對中斷的處理(學習筆記)Linux筆記
- 如何系統學習C 語言(下)之 預處理命令篇
- JAVA學習之異常處理Java
- js學習之非同步處理JS非同步
- Linux系統程式設計之訊號中斷處理(下)Linux程式設計
- Linux系統程式設計之訊號中斷處理(上)Linux程式設計
- Linux學習(三)之系統目錄結構Linux
- sed內建命令字元有什麼功能?linux作業系統學習字元Linux作業系統
- 菜鳥學習計劃淺談之Linux系統Linux
- swoft 學習筆記之異常處理筆記
- Python學習之 異常處理詳解Python
- SpringMVC學習系列(10) 之 異常處理SpringMVC
- 怎樣系統的學習linux?Linux學習路線!Linux
- C#開發PACS醫學影像處理系統(十三):繪圖處理之病灶測量C#繪圖
- Linux系統基礎學習Linux
- 身為學霸的我學習linux系統之基礎篇Linux
- 《奔跑吧 Linux核心》之處理器體系結構Linux
- 如何正確學習Linux系統呢?Linux運維學習Linux運維
- 哪些人適合學習Linux?linux系統使用學習Linux
- C#開發PACS醫學影像處理系統(十二):繪圖處理之圖形標記C#繪圖
- linux學習入門(2使用者處理命令)Linux
- Linux系統有啥優勢?Linux學習Linux
- Linux學習--遠端登入Linux系統Linux
- MyBatis 跳脫字元處理MyBatis字元
- rust學習九.2、集合之字元Rust字元
- Linux系統檔案學習內容多嗎?linux系統命令Linux
- Linux系統有什麼優點?linux作業系統學習Linux作業系統
- Python入門學習之異常處理機制Python
- YOLOv3學習筆記之資料處理YOLO筆記
- 如何最佳化Linux系統安全?Linux學習Linux
- Linux系統學習(四)Linux網路服務Linux
- Linux轉行學習有年齡限制嗎?Linux系統運維學習Linux運維
- Linux學習路線全解,Linux作業系統學習路線Linux作業系統
- 【原創】Linux中斷子系統(二)-通用框架處理Linux框架
- LINUX系統 利用AWK命令處理文字資料過程Linux
- 分享學習linux系統學習心得,從大白變小白Linux
- 學習Linux系統永遠都不晚Linux
- 為什麼要學習Linux系統?Linux
- 06.字元和字串處理字元字串