日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux管道相关命令

發(fā)布時(shí)間:2023/12/20 linux 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux管道相关命令 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

    • (1)cut (提取)
    • (2)sort (排序)
    • (3)wc命令
    • (4)uniq (唯一)
    • (5)tee (管道輸出)
    • (6)tr (替換)
    • (7)split (切割)
    • (8)awk
    • (9)sed

(1)cut (提取)

作用:cut 根據(jù)條件 從命令結(jié)果中 提取 對應(yīng)內(nèi)容

vim 1.txt 內(nèi)容: 111:aaa:bbb:ccc 222:ddd:eee:fff 333:ggg:hhh 444:iii


小結(jié):通過 cut 動作 目標(biāo)文件 可以根據(jù)條件 提取對應(yīng)內(nèi)容

(2)sort (排序)

作用:sort可針對文本文件的內(nèi)容,以行為單位來排序。
第一步: 對字符串排序

cat 表示查看當(dāng)前內(nèi)容 [root@node01 tmp]# cat 2.txt banana apple pear orange pear通過sort排序后 [root@node01 tmp]# sort 2.txt apple banana orange pear pear

第二步: 去重排序

作用:在輸出行中去除重復(fù)行。

[root@node01 tmp]# sort -u 2.txt apple banana orange pear

第三步: 對數(shù)值排序

默認(rèn)按照字符串排序

[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 準(zhǔn)備內(nèi)容

注意看第二列 lisi01,68,99,26 lisi02,98,66,96 lisi03,38,33,86 lisi04,78,44,36 lisi05,88,22,66 lisi06,98,44,46倒序顯示 所有內(nèi)容 [root@node01 tmp]# sort -t ',' -k2nr score.txt結(jié)果: 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命令

第一步: 顯示指定文件 字節(jié)數(shù), 單詞數(shù), 行數(shù) 信息.

準(zhǔn)備內(nèi)容:

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結(jié)果:行數(shù) 單詞數(shù) 字節(jié)數(shù) 文件 [root@hadoop01 export]# wc 4.txt 6 21 85 4.txt

第二步: 只顯示 文件 的行數(shù)

只顯示 文件 的行數(shù) [root@node01 opt]# wc -l 4.txt 6 4.txt

第三步: 統(tǒng)計(jì)多個(gè)文件的 行數(shù) 單詞數(shù) 字節(jié)數(shù)

[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 目錄下 有多少個(gè) 子內(nèi)容

[root@hadoop01 export]# ls /etc | wc -w 240

小結(jié)
通過 wc 文件 就可以 統(tǒng)計(jì) 文件的 字節(jié)數(shù)、單詞數(shù)、行數(shù)

(4)uniq (唯一)

作用:uniq 命令用于檢查及刪除文本文件中重復(fù)出現(xiàn)的行,一般與 sort 命令結(jié)合使用。
第一步:實(shí)現(xiàn)去重效果

# 排序 [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

第二步:不但去重,還要 統(tǒng)計(jì)出現(xiàn)的次數(shù)

要求:去重和統(tǒng)計(jì)出現(xiàn)的次數(shù) [root@hadoop01 export]# cat 5.txt | sort | uniq -c2 李四 1002 麻七 702 王五 901 張三 982 趙六 95

小結(jié)
通過 uniq [選項(xiàng)] 文件 就可以完成 去重行 和 統(tǒng)計(jì)次數(shù)

(5)tee (管道輸出)

將去重統(tǒng)計(jì)的結(jié)果 放到 a.txt、b.txt、c.txt 文件中 [root@hadoop01 export]# cat 5.txt | sort | uniq -c | tee a.txt b.txt c.txt

小結(jié)
通過 tee 可以將命令結(jié)果 通過管道 輸出到 多個(gè)文件

(6)tr (替換)

第一步: 實(shí)現(xiàn) 替換效果

# 將 小寫i 替換成 大寫 I [root@node01 opt]# echo "itheima" | tr 'i' 'I' ItheIma# 把itheima的轉(zhuǎn)換為大寫 [root@node01 opt]# echo "itheima" | tr '[a-z]' '[A-Z]' ITHEIMA# 把 HELLO 轉(zhuǎn)成 小寫 [root@node01 opt]# echo "HELLO" |tr '[A-Z]' '[a-z]' hello

第二步: 實(shí)現(xiàn)刪除效果

需求: 刪除abc1d4e5f中的數(shù)字 [root@node01 opt]# echo 'abc1d4e5f' | tr -d '[0-9]' abcdef

第三步: 單詞計(jì)數(shù)

準(zhǔn)備內(nèi)容 [root@hadoop01 export]# cat words.txt hello,world,hadoop hive,sqoop,flume,hello kitty,tom,jerry,world hadoop1. 將,換成 換行 2. 排序 3. 去重 4. 計(jì)數(shù) # 統(tǒng)計(jì)每個(gè)單詞出現(xiàn)的次數(shù) [root@hadoop01 export]# cat words.txt | tr ',' '\n' | sort | uniq -c1 flume2 hadoop2 hello1 hive1 jerry1 kitty1 sqoop1 tom2 world

(7)split (切割)

第一步: 按 字節(jié) 將 大文件 切分成 若干小文件

[root@node01 opt]# split -b 10k 1.txt

第二步: 按 行數(shù) 將 大文件 切分成 若干小文件

[root@node01 opt]# split -l 5 2.txt

小結(jié)
通過 split 選項(xiàng) 文件名 命令將大文件 切分成 若干小文件

(8)awk

作用:通過 awk 實(shí)現(xiàn) 模糊查詢, 按需提取字段, 還可以進(jìn)行 判斷 和 簡單的運(yùn)算
第一步: 搜索 zhangsan 和 lisi 的成績

第二步: 指定分割符, 根據(jù)下標(biāo)顯示內(nèi)容

第三步: 指定分割符, 根據(jù)下標(biāo)顯示內(nèi)容

第四步: 調(diào)用 awk 提供的函數(shù)

第五步: if語句 查詢及格的學(xué)生信息

第六步: 段內(nèi)容 求學(xué)科平均分

(9)sed

作用:通過 sed 可以實(shí)現(xiàn) 過濾替換 的功能
第一步: 實(shí)現(xiàn) 查詢 功能

準(zhǔn)備數(shù)據(jù):vi 1.txt aaa java root bbb hello ccc rt ddd root nologin eee rtt fff ROOT nologin ggg rttt練習(xí)1 列出 1.txt的 1~5行 的數(shù)據(jù) [root@node01 opt]# sed -n -e '1,5p' 1.txt aaa java root bbb hello ccc rt ddd root nologin eee rtt練習(xí)2 列出1.txt的所有數(shù)據(jù) [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練習(xí)3 列出1.txt的所有數(shù)據(jù) 且 顯示行號 [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練習(xí)4: 查找1.txt中包含root行 [root@node01 opt]# sed -n -e '/root/p' 1.txt aaa java root ddd root nologin 練習(xí)5 列出1.txt中包含root的內(nèi)容,root不區(qū)分大小寫,并顯示行號 [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 練習(xí)6 查找出1.txt中 字母`r`后面是多個(gè)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


第二步: 實(shí)現(xiàn) 刪除 功能

練習(xí)1 刪除01.txt中前3行數(shù)據(jù),并顯示行號 [root@node01 opt]# nl 1.txt | sed -e '1,3d'4 ddd root nologin5 eee rtt6 fff ROOT nologin7 ggg rttt練習(xí)2 保留1.txt中前4行數(shù)據(jù),并顯示行號 [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'

第三步: 實(shí)現(xiàn) 修改 功能

練習(xí)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練習(xí)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

第四步: 實(shí)現(xiàn) 替換 功能

練習(xí)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

練習(xí)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

第五步: 對 原文件 進(jìn)行操作

練習(xí)1 在1.txt中把nologin替換為 huawei [root@node01 opt]# sed -i -e 's/nologin/huawei/' 1.txt練習(xí)2 在1.txt文件中第2、3行替換為aaaaaa [root@node01 opt]# sed -i -e '2,3c aaa' 1.txt練習(xí)3 刪除1.txt中前2行數(shù)據(jù),并且刪除原文件中的數(shù)據(jù) [root@node01 opt]# sed -i -e '1,2d' 1.txt

第六步: 綜合 練習(xí)

練習(xí)1 獲取ip地址 [root@node01 opt]# ifconfig eth0 | grep "inet addr" | sed -e 's/^.*inet addr://' | sed -e 's/Bcast:.*$//' 192.168.100.100 練習(xí)2 從1.txt中提出數(shù)據(jù),匹配出包含root的內(nèi)容,再把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}' #只顯示替換內(nèi)容的行練習(xí)3 從1.txt中提出數(shù)據(jù),刪除前2行,并把nologin替換為itheima,并顯示行號 [root@node01 opt]# nl 1.txt | sed -e '1,2d' | sed -e 's/nologin/itheima/'3 fff ROOT huawei4 ggg rttt

總結(jié)

以上是生活随笔為你收集整理的Linux管道相关命令的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。