Shell—三劍客(grep、sed、awk)


grep命令詳解

文本搜索工具,根據用戶指定的“模式(pattern)”對目標文本進行過濾,顯示被模式匹配到的行。

命令格式:grep  [options]  pattern  filename。grep適合單純的查找或匹配文本。grep是區分大小寫的。

匹配參數[options]

  • -i     不區分大小寫,忽略字符大小寫
  • -v    后面接啥排除啥,取反,顯示不被pattern匹配到的行
  • -n    顯示匹配結果的行號
  • -c    統計匹配結果的行數
  • -o    僅顯示匹配到的字符串,不把整行顯示出來
  • -e    實現多個選項的匹配,邏輯or關系
  • -q    靜默模式,不輸出任何信息。與"echo $"合用,查看是否匹配到,0表示匹配到,1表示沒有匹配到
  • -Ax:顯示匹配結果所在行以及該行之后的指定行數,x是行數,A:after。
  • -Bx:顯示匹配結果所在行以及該行之前的指定行數,x是行數,B:before。
  • -Cx:顯示匹配結果所在行以及該行之前和該行之后的指定行數,x是行數,C:context
  • --color             顯示顏色
  • -E 使用ERE,相當於egrep
[root@localhost ~]# grep "root" /etc/passwd      # 找到root所在的所有行並顯示
[root@localhost ~]# grep -v "root" /etc/passwd   # 找到除root外的所有行並顯示
[root@localhost ~]# grep -n "root" /etc/passwd   # 顯示行號
[root@localhost ~]# grep -c "root" /etc/passwd   # 顯示匹配結果的行數
[root@localhost ~]# grep -A2 "root" /etc/passwd  # 匹配含有root的行,以及該行的后兩行
[root@localhost ~]# grep -e "root" -e "myuser" /etc/passwd

sed命令詳解

sed 是一種在線的、非交互式的編輯器,它一次處理一行內容。處理時,把當前處理的行存儲在臨時緩沖區中,稱為“模式空間”(pattern space),接着用sed命令處理緩沖區中的內容,處理完成后,把緩沖區的內容送往屏幕。接着處理下一行,這樣不斷重復,直到文件末尾。文件內容並沒有改變,除非你使用重定向存儲輸出。
sed 主要用來自動編輯一個或多個文件;簡化對文件的反復操作;編寫轉換程序等。
sed 和grep不一樣,不管是否找到指定的模式,它的退出狀態都是0。只有當命令存在語法錯誤時,sed的退出狀態才是非0。

實現數據的替換,刪除,增加,選取等(以行為單位進行處理)

1.打印顯示文本內容

[root@localhost ~]# sed -n '3p' test.sh        # 打印文件的第3行。
[root@localhost ~]# sed -n '$p' test.sh        # 打印文件的最后一行
[root@localhost ~]# sed -n '3,6p' test.sh      # 打印文件的第3行到第6行。
[root@localhost ~]# sed -n '3,$p' test.sh      # 打印文件的第3行到最后一行的內容。
[root@localhost ~]# sed -n '3~2p' test.sh      # 從第3行開始,每隔兩行打印一行,波浪號后面的2表示步長。

[root@localhost ~]# sed -n '/love/p' test.sh    #  逐行讀取文件,打印匹配love的行
[root@localhost ~]# sed -n '/love/,3p' test.sh  # 逐行讀取文件,打印從匹配love的行到第3行的內容,也打印后面所有匹配love 的行。
[root@localhost ~]# sed -n '/love/,$p' test.sh  # 逐行讀取文件,打印從匹配too的行到最后一行的內容。
[root@localhost ~]# sed -n '/love/,+1p'  test.sh    #打印匹配too的行及其向后一行,如果有多行匹配too,則匹配的每一行都會向后多打印一行
[root@localhost ~]# sed -n '/love/,/you/p'  1.txt   #打印從匹配內容love到匹配內容you的行
[root@localhost ~]# sed -n '3,/love/p' test.sh  # 打印第三行到匹配love的行

# 打印test.sh文件最后一行的行號(即文件有多少行,和wc -l 功能類似)
[root@localhost ~]# sed -n "$=" test.sh
# 打印匹配error的行的行號
[root@localhost ~]# sed -n '/error/='  test.sh    
# 打印匹配error的行的行號和內容(可用於查看日志中有error的行及其內容)
[root@localhost ~]# sed -n '/error/{=;p}'   test.sh

2.增加文件內容,向文件中添加或插入行

# 在第三行后面添加python,3表示行號
[root@localhost ~]# sed '3apython' test.sh
# 在第三行之前插入python,3表示行號
[root@localhost ~]# sed '3ipython' test.sh

# 在最后一行之后添加python
[root@localhost ~]# sed '$apython' test.sh
# 在最后一行之前插入python
[root@localhost ~]# sed '$ipython' test.sh

# 在包含123的行后面添加python,如果文件中有多行包括123,則每一行后面都會添加
[root@localhost ~]# sed '/123/apython' test.sh
# 在包含123的行之前插入python,如果文件中有多行包含123,則每一行之前都會插入
[root@localhost ~]# sed '/123/ipython'  test.sh  

3.刪除文件中指定的行

[root@localhost ~]# sed '3d' 1.txt       # 刪除第三行
[root@localhost ~]# sed '$d' 1.txt       # 刪除最后一行
[root@localhost ~]# sed '1~2d' 1.txt     # 從第一行開始刪除,每隔2行就刪掉一行,即刪除奇數行
[root@localhost ~]# sed '1,3d'  1.txt    # 刪除1~3行
[root@localhost ~]# sed '1,3!d'  1.txt   # 刪除1~3之外的所有行

[root@localhost ~]# sed '/123/d'  1.txt       # 刪除匹配123的行
[root@localhost ~]# sed '/123/,$d' 1.txt      # 刪除從匹配123的行到最后一行
[root@localhost ~]# sed '/123/,+1d' 1.txt     # 刪除匹配123的行及其后面一行
[root@localhost ~]# sed '/^$/d'   1.txt       # 刪除空行
[root@localhost ~]# sed '/123\|abc/!d' 1.txt  # 刪除不匹配123或abc的行,/123\|abc/ 表示匹配123或abc ,!表示取反

4.更改文件中指定的行

[root@localhost ~]# sed '1chello' test.sh         # 將文件的第一行替換為hello   
[root@localhost ~]# sed '$chello' test.sh         # 將文件的最后一行替換為hello
[root@localhost ~]# sed '/123/chello' test.sh     # 將包含123的行替換為hello

https://www.runoob.com/linux/linux-comm-sed.htmlhttps://www.cnblogs.com/ftl1012/p/9250438.htmlhttps://blog.csdn.net/wdz306ling/article/details/80087889https://www.jianshu.com/p/d9f40945242bhttps://blog.csdn.net/a13822665196/article/details/102171573

awk命令詳解


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM