文章目錄
- (1)cut (提取)
- (2)sort (排序)
- (3)wc命令
- (4)uniq (唯一)
- (5)tee (管道輸出)
- (6)tr (替換)
- (7)split (切割)
- (8)awk
- (9)sed
(1)cut (提取)
作用:cut 根據條件 從命令結果中 提取 對應內容
vim 1.txt 內容:
111:aaa:bbb:ccc
222:ddd:eee:fff
333:ggg:hhh
444:iii
小結:通過 cut 動作 目標文件 可以根據條件 提取對應內容
(2)sort (排序)
作用:sort可針對文本文件的內容,以行為單位來排序。
第一步: 對字符串排序
cat 表示查看當前內容
[root@node01 tmp]# cat 2.txt
banana
apple
pear
orange
pear通過sort排序后
[root@node01 tmp]# sort 2.txt
apple
banana
orange
pear
pear
第二步: 去重排序
作用:在輸出行中去除重復行。
[root@node01 tmp]# sort -u 2.txt
apple
banana
orange
pear
第三步: 對數值排序
默認按照字符串排序
[root@node01 tmp]# sort 2.txt
1
10
11
2
3
4
5
6
7
8
9
升序
[root@node01 tmp]# sort -n 2.txt
1
2
3
4
5
6
7
8
9
10
11
倒序
[root@node01 tmp]# sort -n -r 2.txt
11
10
9
8
7
6
5
4
3
2
1
倒序-合并式
[root@node01 tmp]# sort -nr 2.txt
11
10
9
8
7
6
5
4
3
2
1
第四步: 對成績排序
vim score.txt 準備內容
注意看第二列
lisi01,68,99,26
lisi02,98,66,96
lisi03,38,33,86
lisi04,78,44,36
lisi05,88,22,66
lisi06,98,44,46倒序顯示 所有內容
[root@node01 tmp]# sort -t ',' -k2nr score.txt結果:
lisi02,98,66,96
lisi06,98,44,46
lisi05,88,22,66
lisi04,78,44,36
lisi01,68,99,26
lisi03,38,33,86
(3)wc命令
第一步: 顯示指定文件 字節數, 單詞數, 行數 信息.
準備內容:
cat 查看文本
[root@hadoop01 export]# cat 4.txt
111
222 bbb
333 aaa bbb
444 aaa bbb ccc
555 aaa bbb ccc ddd
666 aaa bbb ccc ddd eee結果:行數 單詞數 字節數 文件
[root@hadoop01 export]# wc 4.txt 6 21 85 4.txt
第二步: 只顯示 文件 的行數
只顯示 文件 的行數
[root@node01 opt]# wc -l 4.txt
6 4.txt
第三步: 統計多個文件的 行數 單詞數 字節數
[root@hadoop01 export]# wc 1.txt 2.txt 3.txt 4 4 52 1.txt11 11 24 2.txt6 21 85 3.txt21 36 161 總用量[root@hadoop01 export]# wc *.txt4 4 52 1.txt11 11 24 2.txt6 21 85 3.txt6 6 95 score.txt27 42 256 總用量
第四步: 查看 /etc 目錄下 有多少個 子內容
[root@hadoop01 export]# ls /etc | wc -w
240
小結
通過 wc 文件 就可以 統計 文件的 字節數、單詞數、行數
(4)uniq (唯一)
作用:uniq 命令用于檢查及刪除文本文件中重復出現的行,一般與 sort 命令結合使用。
第一步:實現去重效果
# 排序
[root@hadoop01 export]# cat 5.txt | sort
李四 100
李四 100
麻七 70
麻七 70
王五 90
王五 90
張三 98
趙六 95
趙六 95# 去重
[root@hadoop01 export]# cat 5.txt | sort | uniq
李四 100
麻七 70
王五 90
張三 98
趙六 95
第二步:不但去重,還要 統計出現的次數
要求:去重和統計出現的次數
[root@hadoop01 export]# cat 5.txt | sort | uniq -c2 李四 1002 麻七 702 王五 901 張三 982 趙六 95
小結
通過 uniq [選項] 文件 就可以完成 去重行 和 統計次數
(5)tee (管道輸出)
將去重統計的結果 放到 a.txt、b.txt、c.txt 文件中
[root@hadoop01 export]# cat 5.txt | sort | uniq -c | tee a.txt b.txt c.txt
小結
通過 tee 可以將命令結果 通過管道 輸出到 多個文件中
(6)tr (替換)
第一步: 實現 替換效果
# 將 小寫i 替換成 大寫 I
[root@node01 opt]# echo "itheima" | tr 'i' 'I'
ItheIma# 把itheima的轉換為大寫
[root@node01 opt]# echo "itheima" | tr '[a-z]' '[A-Z]'
ITHEIMA# 把 HELLO 轉成 小寫
[root@node01 opt]# echo "HELLO" |tr '[A-Z]' '[a-z]'
hello
第二步: 實現刪除效果
需求: 刪除abc1d4e5f中的數字
[root@node01 opt]# echo 'abc1d4e5f' | tr -d '[0-9]'
abcdef
第三步: 單詞計數
準備內容
[root@hadoop01 export]# cat words.txt
hello,world,hadoop
hive,sqoop,flume,hello
kitty,tom,jerry,world
hadoop1. 將,換成 換行 2. 排序 3. 去重 4. 計數
# 統計每個單詞出現的次數
[root@hadoop01 export]# cat words.txt | tr ',' '\n' | sort | uniq -c1 flume2 hadoop2 hello1 hive1 jerry1 kitty1 sqoop1 tom2 world
(7)split (切割)
第一步: 按 字節 將 大文件 切分成 若干小文件
[root@node01 opt]# split -b 10k 1.txt
第二步: 按 行數 將 大文件 切分成 若干小文件
[root@node01 opt]# split -l 5 2.txt
小結
通過 split 選項 文件名 命令將大文件 切分成 若干小文件
(8)awk
作用:通過 awk 實現 模糊查詢, 按需提取字段, 還可以進行 判斷 和 簡單的運算等
第一步: 搜索 zhangsan 和 lisi 的成績
第二步: 指定分割符, 根據下標顯示內容
第三步: 指定分割符, 根據下標顯示內容
第四步: 調用 awk 提供的函數
第五步: if語句 查詢及格的學生信息
第六步: 段內容 求學科平均分
(9)sed
作用:通過 sed 可以實現 過濾 和 替換 的功能
第一步: 實現 查詢 功能
準備數據:vi 1.txt
aaa java root
bbb hello
ccc rt
ddd root nologin
eee rtt
fff ROOT nologin
ggg rttt練習1 列出 1.txt的 1~5行 的數據
[root@node01 opt]# sed -n -e '1,5p' 1.txt
aaa java root
bbb hello
ccc rt
ddd root nologin
eee rtt練習2 列出1.txt的所有數據
[root@node01 opt]# sed -n -e '1,$p' 1.txt
aaa java root
bbb hello
ccc rt
ddd root nologin
eee rtt
fff ROOT nologin
ggg rttt練習3 列出1.txt的所有數據 且 顯示行號
[root@node01 opt]# sed -n -e '1,$=' -e '1,$p' 1.txt
1 aaa java root
2 bbb hello
3 ccc rt
4 ddd root nologin
5 eee rtt
6 fff ROOT nologin
7 ggg rttt其他方法
簡化版
1:cat -n 1.txt
2:cat -b 1.txt
3:nl 1.txt練習4: 查找1.txt中包含root行
[root@node01 opt]# sed -n -e '/root/p' 1.txt
aaa java root
ddd root nologin
練習5 列出1.txt中包含root的內容,root不區分大小寫,并顯示行號
[root@node01 opt]# nl 1.txt | sed -n -e '/root/Ip'1 aaa java root4 ddd root nologin6 fff ROOT nologin其他方法:1. nl 1.txt | grep -i root2. cat -n 1.txt | grep -i root
練習6 查找出1.txt中 字母`r`后面是多個t的行,并顯示行號
[root@node01 opt]# nl 1.txt | sed -nr -e '/r+t/p'3 ccc rt5 eee rtt7 ggg rttt或者 sed -nr -e '/r+t/p' -e '/r+t/=' 1.txt
第二步: 實現 刪除 功能
練習1 刪除01.txt中前3行數據,并顯示行號
[root@node01 opt]# nl 1.txt | sed -e '1,3d'4 ddd root nologin5 eee rtt6 fff ROOT nologin7 ggg rttt練習2 保留1.txt中前4行數據,并顯示行號
[root@node01 opt]# nl 1.txt | sed -e '5,$d'1 aaa java root2 bbb hello3 ccc rt4 ddd root nologin或者 nl 1.txt | sed -n -e '1,4p'
第三步: 實現 修改 功能
練習1: 在1.txt的第二行后添加aaaaa,并顯示行號
[root@node01 opt]# nl 1.txt | sed -e '2a aaaaa'1 aaa java root2 bbb hello
aaaaa3 ccc rt4 ddd root nologin5 eee rtt6 fff ROOT nologin7 ggg rttt練習2 在1.txt的第1行前添加bbbbb,并顯示行號
[root@node01 opt]# nl 1.txt | sed -e '1i bbbbb'
bbbbb1 aaa java root2 bbb hello3 ccc rt4 ddd root nologin5 eee rtt6 fff ROOT nologin7 ggg rttt
第四步: 實現 替換 功能
練習1 把1.txt中的nologin替換成為huawei,并顯示行號
[root@node01 opt]# nl 1.txt | sed -e 's/nologin/huawei/'1 aaa java root2 bbb hello3 ccc rt4 ddd root huawei5 eee rtt6 fff ROOT huawei7 ggg rttt
練習2 把1.txt中的1,2行替換為aaa,并顯示行號
[root@node01 opt]# nl 1.txt passwd | sed -e '1,2c aaa'
nl: passwdaaa3 ccc rt4 ddd root nologin5 eee rtt6 fff ROOT nologin7 ggg rttt
第五步: 對 原文件 進行操作
練習1 在1.txt中把nologin替換為 huawei
[root@node01 opt]# sed -i -e 's/nologin/huawei/' 1.txt練習2 在1.txt文件中第2、3行替換為aaaaaa
[root@node01 opt]# sed -i -e '2,3c aaa' 1.txt練習3 刪除1.txt中前2行數據,并且刪除原文件中的數據
[root@node01 opt]# sed -i -e '1,2d' 1.txt
第六步: 綜合 練習
練習1 獲取ip地址
[root@node01 opt]# ifconfig eth0 | grep "inet addr" | sed -e 's/^.*inet addr://' | sed -e 's/Bcast:.*$//'
192.168.100.100 練習2 從1.txt中提出數據,匹配出包含root的內容,再把nologin替換為itheima
[root@node01 opt]# nl 1.txt | grep 'root' | sed -e 's/nologin/itheima/'1 ddd root huawei或者1. nl 01.txt | sed -n -e '/root/p' | sed -e 's/nologin/itheima/'2. nl 01.txt | sed -n -e '/root/{s/nologin/itheima/p}' #只顯示替換內容的行練習3 從1.txt中提出數據,刪除前2行,并把nologin替換為itheima,并顯示行號
[root@node01 opt]# nl 1.txt | sed -e '1,2d' | sed -e 's/nologin/itheima/'3 fff ROOT huawei4 ggg rttt
總結
以上是生活随笔為你收集整理的Linux管道相关命令的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。