1. 程式人生 > >張明貴-Linux基礎命令學習-3

張明貴-Linux基礎命令學習-3

images awk inux運維 mark 微軟 1.5 學習 image -i

【Linux運維是應用學科,多練就會有結果】






只查看test.txt文件(50行)內第20到30行的內容

建立實驗環境

[root@xwj ~]# seq 50 >test.txt

[root@xwj ~]# cat test.txt


方法一:

[root@xwj ~]# head -30 test.txt|tail -11

技術分享圖片


方法二:

sed stream editor 流編輯器 實現對文件的增、刪、改、替換、查

[root@xwj ~]# sed -n '20,30'p test.txt

技術分享圖片

方法三:

awk 過濾輸出內容 處理日誌文本 NR表示行號

[root@xwj ~]# awk '19<NR && NR<31' test.txt

技術分享圖片

查看第10行的內容

[root@xwj ~]# awk 'NR==10' test.txt


方法四:

使用grep 篩選

grep 20 -A -n test.txt 除了匹配20這一行之外,並顯示後n行

grep 30 -B -n test.txt 除了匹配30這一行之外,並顯示前n行

grep 25 -C -n test.txt 除了匹配30這一行之外,並顯示前後各n行

[root@xwj ~]# grep 20 -A 10 test.txt

技術分享圖片

[root@xwj ~]# grep 30 -B 10 test.txt

[root@xwj ~]# grep 25 -C 5 test.txt







seq 序列

[root@xwj ~]# seq 10

[root@xwj ~]# seq 5 10

[root@xwj ~]# seq 10 -2 0

[root@xwj ~]# seq 10 2 20

技術分享圖片

1-10橫著寫

技術分享圖片







把/zmg目錄及其子目錄下所有以擴展名是.sh結尾的文集中包含xinwanjia的字符全部替換為xwj

sed [-i] 's#A#B#g' A為替換的內容,B為替換成的內容,有參數i會修改文件的內容,反之則不會修個內容

技術分享圖片



張明貴-Linux基礎命令學習-3