Shell編程之正則表達式(二)

文本處理器

Linux/UNIX 系統中包含很多種文本處理器或文本編輯器,其中包括我們之前學習過的VIM 編輯器與 grep 等。而 grep、sed、awk 更是 shell 編程中經常用到的文本處理工具,被稱之爲 Shell 編程三劍客。

sed 工具

sed(Stream EDitor)是一個強大而簡單的文本解析轉換工具,可以讀取文本,並根據指定的條件對文本內容進行編輯(刪除、替換、添加、移動等),最後輸出所有行或者僅輸出處理的某些行。sed 也可以在無交互的情況下實現相複雜的文本處理操作,被廣泛應用於 Shell腳本中,用以完成各種自動化處理任務。

  • sed 的工作流程主要包括讀取、執行和顯示三個過程

    • 讀取:sed 從輸入流(文件、管道、標準輸入)中讀取一行內容並存儲到臨時的緩 衝區中(又稱模式空間,pattern space
    • 執行:默認情況下,所有的 sed 命令都在模式空間中順序地執行,除非指定了行的地址,否則 sed命令將會在所有的行上依次執行
    • 顯示:發送修改後的內容到輸出流。再發送數據後,模式空間將會被清空
    • 在所有的文件內容都被處理完成之前,上述過程將重複執行,直至所有內容被處理完
  • 默認情況下,所有的 sed 命令都是在模式空間內執行的,因此輸入的文件並不會發生任何變化,除非是用重定向存儲輸出

sed 命令常見用法

  • sed 命令有兩種格式:

    sed [選項] '操作' 參數        //“參數”是指操作的目標文件,當存在多個操作對象時用,文件之間用逗號“,”分隔
    或
    sed [選項] -f scriptfile 參數   // scriptfile 表示腳本文件,需要用“-f”選項指定
  • 常見sed命令選項

    • -e--expression=:表示用指定命令或者腳本來處理輸入的文本文件
    • -f--file=:表示用指定的腳本文件來處理輸入的文本文件
    • -h--help:顯示幫助
    • -n--quietsilent:表示僅顯示處理後的結果
    • -i:直接編輯文本文件
  • “操作”用於指定對文件操作的動作行爲,也就是sed 的命令。通常情況下是採用的“[n1[,n2]]”操作參數的格式。n1、n2 是可選的,不一定會存在,代表選擇進行操作的行數,如操作需要在 5~20 行之間進行,則表示爲“5,20 動作行爲”。常見的操作包括以下幾種
    • a:增加,在當前行下面增加一行指定內容
    • c:替換,將選定行替換爲指定內容
    • d:刪除,刪除選定的行
    • i:插入,在選定行上面插入一行指定內容
    • p:打印,如果同時指定行,表示打印指定行;如果不指定行,則表示打印所有內容;如果有非打印字符,則以 ASCII 碼輸出。其通常與“-n”選項一起使用
    • s:替換,替換指定字符
    • y:字符轉換

示例

1)輸出符合條件的文本(p 表示正常輸出)

[root@localhost opt]# sed -n 'p' httpd.txt     //輸出文件所有內容,等同 cat httpd.txt
#
# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
# In particular, see 
# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
# for a discussion of each configuration directive.
#
# Do NOT simply read the instructions in here without understanding
# what they do.  They're here only as hints or reminders.  If you are unsure
# consult the online docs. You have been warned.  
#
# Configuration and logfile names: If the filenames you specify for many
# of the server's control files begin with "/" (or "drive:/" for Win32), the
# server will use that explicit path.  If the filenames do *not* begin
# with "/", the value of ServerRoot is prepended -- so 'log/access_log'
# with ServerRoot set to '/www' will be interpreted by the
# server as '/www/log/access_log', where as '/log/access_log' will be
...//省略部分內容....
[root@localhost opt]# sed -n '3p' httpd.txt     //輸出第3行內容
# configuration directives that give the server its instructions.
[root@localhost opt]# sed -n '3,5p' httpd.txt    //輸出3~5行內容
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
# In particular, see 
[root@localhost opt]# sed -n 'p;n' httpd.txt      //輸出所有奇數行內容,n表示讀入下一行資料
#
# configuration directives that give the server its instructions.
# In particular, see 
# for a discussion of each configuration directive.
# Do NOT simply read the instructions in here without understanding
# consult the online docs. You have been warned.  
# Configuration and logfile names: If the filenames you specify for many
# server will use that explicit path.  If the filenames do *not* begin
# with ServerRoot set to '/www' will be interpreted by the
# interpreted as '/log/access_log'.
...//省略部分內容....
[root@localhost opt]# sed -n 'n;p' httpd.txt       //輸出所有偶數行
# This is the main Apache HTTP server configuration file.  It contains the
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
#
# what they do.  They're here only as hints or reminders.  If you are unsure
#
# of the server's control files begin with "/" (or "drive:/" for Win32), the
...//省略部分內容....
[root@localhost opt]# sed -n '1,5{p;n}' httpd.txt    //輸出1~5行之間的奇數行(1、3、5行) 
#
# configuration directives that give the server its instructions.
# In particular, see 
[root@localhost opt]# sed -n '350,${n;p}' httpd.txt   //輸出第350行至文件尾之間的偶數行
#
IncludeOptional conf.d/*.conf
short
wod
woooood
AxyzxyzC
//在執行此命令時,讀取的第1行時第350行,讀取的第二行是第351行,依次類推,所以輸出的偶數行是文件的第351行、第353行直至文件結尾,其中包括空行
  • sed 命令結合正則表達式時,格式略有不同,正則表達式以“/”包圍
[root@localhost opt]# sed -n '/the/p' httpd.txt        //輸出包含the的行
# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
# Do NOT simply read the instructions in here without understanding
# what they do.  They're here only as hints or reminders.  If you are unsure
# consult the online docs. You have been warned.  
# Configuration and logfile names: If the filenames you specify for many
# of the server's control files begin with "/" (or "drive:/" for Win32), the
# server will use that explicit path.  If the filenames do *not* begin
# with "/", the value of ServerRoot is prepended -- so 'log/access_log'
# with ServerRoot set to '/www' will be interpreted by the
[root@localhost opt]# sed -n '4,/the/p' httpd.txt     //輸出從第4行至第一個包含the的行
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
# In particular, see 
# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
# for a discussion of each configuration directive.
#
# Do NOT simply read the instructions in here without understanding
[root@localhost opt]# sed -n '/the/=' httpd.txt     //輸出包含the的所在行的行號,等號(=)用來輸出行號
2
3
9
10
11
13
14
...//省略部分內容...
[root@localhost opt]# sed -n '/^sh/p' httpd.txt    //輸出以sh開頭的行
shirt
short
[root@localhost opt]# sed -n '/[0-9]$/p' httpd.txt     //輸出以數字結尾的行
#Listen 12.34.56.78:80
Listen 80
#ServerName www.example.com:80
AddDefaultCharset UTF-8
[root@localhost opt]# sed -n '/\<wood\>/p' httpd.txt      //輸出包含的單詞wood的行,\<\>代表單詞邊界
wood

2)刪除符合條件的文本(d)

  • 下面命令中 nl 命令用於計算文件的行數,結合該命令可以更加直觀地查看到命令執行的結果。
[root@localhost opt]# nl httpd.txt | sed '3d'      //刪除第3行
     1  u
     2  # This is the main Apache HTTP server configuration file.  It contains the
     4  # See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
     5  # In particular, see 
     6  # <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
     7  # for a discussion of each configuration directive.
...//省略部分內容...
[root@localhost opt]# nl httpd.txt | sed '3,5d'     //刪除3~5行
     1  u
     2  # This is the main Apache HTTP server configuration file.  It contains the
     6  # <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
     7  # for a discussion of each configuration directive.
     ...//省略部分內容...
[root@localhost opt]# nl httpd.txt | sed '/wood/d'     //刪除包含wood的行,原第321行被刪除
     1  u                                     //刪除不包含cross 的行,用!符號表示取反操作,如'/cross/!d'
     2  # This is the main Apache HTTP server configuration file.  It contains the
     3  # configuration directives that give the server its instructions.
     ...//省略部分內容...
   318  short
   319  wd
   320  wod
   322  woooood
   323  AxyzC
   324  AxyzxyzC 
[root@localhost opt]# sed '/^[a-z]/d' httpd.txt     //刪除以小寫字母開頭的行
# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
...//省略部分內容...
# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf
AxyzC
AxyzxyzC
[root@localhost opt]# sed '/\.$/d' httpd.txt    //刪除以“.”結尾的行
u
# This is the main Apache HTTP server configuration file.  It contains the
# In particular, see 
# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
...//省略部分內容...
[root@localhost opt]# sed '/^$/d' httpd.txt      //刪除所有空行
u
# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
# In particular, see 
# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
# for a discussion of each configuration directive.
//注意: 若是刪除重複的空行,即連續的空行只保留一個, 執行“ sed –e ‘/^$/{n;/^$/d}’ httpd.txt”命令即可實現。其效果與“cat -s test.txt”相同,n 表示讀下一行數據。

3)替換符合條件的文本

  • 在使用 sed 命令進行替換操作時需要用到 s(字符串替換)、c(整行/整塊替換)、y(字符轉換)命令選項
[root@localhost opt]# sed 's/the/THE/' httpd.txt     //將每行中第一個the替換爲THE
u
# This is THE main Apache HTTP server configuration file.  It contains the
# configuration directives that give THE server its instructions.
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
# In particular, see 
...//省略部分內容...
[root@localhost opt]# vim aaa.txt       //編輯一個新文件
llllll
llllll
llllll                                  //編輯內容
llllll
llllll
llllll
~                                                                             
~                                                                             
:wq                                     //保存退出
[root@localhost opt]# sed 's/l/L/3' aaa.txt     //將每行中第3個l替換爲L
llLlll
llLlll                                           //顯示替換後內容
llLlll
llLlll
llLlll
llLlll
[root@localhost opt]# sed 's/the/THE/g' httpd.txt    //將文件中多有的the替換爲THE
u
# This is THE main Apache HTTP server configuration file.  It contains THE
# configuration directives that give THE server its instructions.
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
# In particular, see 
...//省略部分內容...
[root@localhost opt]# sed 's/o//g' httpd.txt      //將文件中所有的o刪除(替換爲空串)
u
# This is the main Apache HTTP server cnfiguratin file.  It cntains the
# cnfiguratin directives that give the server its instructins.
...//省略部分內容...
shirt
shrt
wd
wd
wd
wd
AxyzC
AxyzxyzC
[root@localhost opt]# sed 's/^/#/' aaa.txt     //在每行行首插入#號
#llllll
#llllll
#llllll
#llllll
#llllll
#llllll
[root@localhost opt]# sed 's/$/eof/' aaa.txt     //在每行行尾插入字符串eof
lllllleof
lllllleof
lllllleof
lllllleof
lllllleof
lllllleof
[root@localhost opt]# vim aaa.txt        //編輯文件
llllll
llllll
llllll
llllll
llllll
llllll
aaaaaa
aaaaaa                        //添加內容
aaaaaa
aaaaaa
aaaaaa
~                                                                             
~                                                                             
:wq                            //保存退出
[root@localhost opt]# sed '/a/s/^/#/' aaa.txt         //在包含 a 的每行行首插入#號
llllll
llllll
llllll
llllll
llllll
llllll
#aaaaaa
#aaaaaa
#aaaaaa
#aaaaaa
#aaaaaa
[root@localhost opt]# sed '3,5s/l/L/g' aaa.txt    //將第 3~5 行中的所有 l 替換爲 L
llllll
llllll
LLLLLL
LLLLLL
LLLLLL
llllll
aaaaaa
aaaaaa
aaaaaa
aaaaaa
aaaaaa
[root@localhost opt]# vim bbb.txt    //編輯一個新文件
this is
the wood
wood
wod                             //編輯內容
the wood
this is test
~                                                                             
~                                                                             
:wq                               //保存退出
[root@localhost opt]# sed '/the/s/o/O/g' bbb.txt    //將包含 the 的所有行中的 o 都替換爲 O
this is
the wOOd
wood
wod
the wOOd
this is test

4) 遷移符合條件的文本

  • H,複製到剪貼板;gG,將剪貼板中的數據覆蓋/追加至指定行;w,保存爲文件;r,讀取指定文件;a,追加指定內容。
[root@localhost opt]# sed '/the/{H;d};$G' bbb.txt    //將包含the 的行遷移至文件末尾,{;}用於多個操作
this is
wood
wod
this is test

the wood
the wood
[root@localhost opt]# sed '1,3{H;d};8G' aaa.txt     //將1~3行內容遷移到8行之後
llllll
llllll
llllll
aaaaaa
aaaaaa

llllll
llllll
llllll
aaaaaa
aaaaaa
aaaaaa
[root@localhost opt]# sed '/the/w ccc.txt' bbb.txt     //將包含the 的行另存爲文件ccc.txt
this is
the wood
wood
wod
the wood
this is test
[root@localhost opt]# cat ccc.txt                  //查看保存的文件內容
the wood
the wood
[root@localhost opt]# sed '/the/r /etc/hostname' bbb.txt   
this is                                  //將文件/etc/hostname 的內容添加到包含the 的每行以後
the wood
localhost.localdomain
wood
wod
the wood
localhost.localdomain
this is test
[root@localhost opt]# sed '3aNEW' bbb.txt   //在第 3 行後插入一個新行,內容爲 NEW
this is
the wood
wood
NEW
wod
the wood
this is test
[root@localhost opt]# sed '/the/aNEW' bbb.txt     //在包含the 的每行後插入一個新行,內容爲 NEW
this is
the wood
NEW
wood
wod
the wood
NEW
this is test
[root@localhost opt]# sed '3aNEW\nNEW2' bbb.txt  //在第 3 行後插入多行內容,中間的\n 表示換行
this is
the wood
wood
NEW
NEW2
wod
the wood
this is test

5) 使用腳本編輯文件

  • 使用 sed 腳本,將多個編輯指令存放到文件中(每行一條編輯指令),通過“-f”選項來調用。
sed '1,3{H;d};6G' bbb.txt    //將1~3行的內容遷移到6行之後

[root@localhost opt]# vim test
1,3H
1,3d
6G
~                                                                            
:wq 
[root@localhost opt]# sed -f test bbb.txt 
wod
the wood
this is test

this is
the wood
wood

6) sed 直接操作文件示例

  • 編寫一個腳本,用來調整 vsftpd 服務配置:禁止匿名用戶,但允許本地用戶(也允許寫入)。
[root@localhost ~]# **vim local_only_ftp.sh** 
#!/bin/bash
#指定樣本文件路徑、配置文件路徑
SAMPLE="/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/vsftpd.conf " CONFIG="/etc/vsftpd/vsftpd.conf"
#備份原來的配置文件,檢測文件名爲/etc/vsftpd/vsftpd.conf.bak 備份文件是否存在, 若不存在則使用 cp 命令進行文件備份
[ ! -e "$CONFIG.bak" ] && cp $CONFIG $CONFIG.bak      //基於樣本配置進行調整,覆蓋現有文件
sed -e '/^anonymous_enable/s/YES/NO/g' $SAMPLE > $CONFIG
sed -i -e '/^local_enable/s/NO/YES/g' -e '/^write_enable/s/NO/YES/g' $CONFIG grep "listen" $CONFIG || sed -i '$alisten=YES' $CONFIG
# 啓動vsftpd 服務,並設爲開機後自動運行systemctl restart vsftpd
systemctl enable vsftpd
[root@localhost ~]# **chmod +x local_only_ftp.sh
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章