在 Linux/UNIX
系統中包含不少種文本處理器或文本編輯器,其中包括咱們以前學習過的VIM
編輯器與 grep
等。而 grep、sed、awk
更是 shell
編程中常常用到的文本處理工具,被稱之爲 Shell
編程三劍客。html
sed(Stream EDitor)
是一個強大而簡單的文本解析轉換工具,能夠讀取文本,並根據指定的條件對文本內容進行編輯(刪除、替換、添加、移動等),最後輸出全部行或者僅輸出處理的某些行。sed
也能夠在無交互的狀況下實現相複雜的文本處理操做,被普遍應用於 Shell
腳本中,用以完成各類自動化處理任務。正則表達式
sed
的工做流程主要包括讀取、執行和顯示三個過程shell
sed
從輸入流(文件、管道、標準輸入)中讀取一行內容並存儲到臨時的緩 衝區中(又稱模式空間,pattern space
)sed
命令都在模式空間中順序地執行,除非指定了行的地址,不然 sed
命令將會在全部的行上依次執行sed 命令有兩種格式:express
sed [選項] '操做' 參數 //「參數」是指操做的目標文件,當存在多個操做對象時用,文件之間用逗號「,」分隔 或 sed [選項] -f scriptfile 參數 // scriptfile 表示腳本文件,須要用「-f」選項指定
常見sed命令選項apache
-e
或--expression=
:表示用指定命令或者腳原本處理輸入的文本文件-f
或--file=
:表示用指定的腳本文件來處理輸入的文本文件-h
或--help
:顯示幫助-n
、--quiet
或silent
:表示僅顯示處理後的結果-i
:直接編輯文本文件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行之間的奇數行(一、三、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)vim
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)替換符合條件的文本bash
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) 遷移符合條件的文本dom
H
,複製到剪貼板;g
、G
,將剪貼板中的數據覆蓋/追加至指定行;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