一君编、sed介紹
sed 是一種在線的服协、非交互式的編輯器,它一次處理一行內(nèi)容啦粹。
處理時,先把當前處理的行內(nèi)容存儲在臨時緩沖區(qū)中窘游,稱為“模式空間”(pattern space)唠椭。
之后再用sed命令處理緩沖區(qū)中的內(nèi)容,處理完成后忍饰,把緩沖區(qū)的內(nèi)容打印到屏幕贪嫂。
接著處理下一行,這樣不斷重復(fù)艾蓝,直到文件末尾力崇。
注意:模式空間的內(nèi)容和 AWK 中的 $0 是一樣的,處理每行的時候赢织,都會被重新賦值為當前行的內(nèi)容
文件內(nèi)容并沒有改變亮靴,除非你使用重定向存儲輸出。
Sed主要用來自動編輯一個或多個文件于置;簡化對文件的反復(fù)操作茧吊;編寫轉(zhuǎn)換程序等。
二八毯、命令格式
[root@linux-server ~]# cat test.txt
1:root:x:0:0:root:/root:/bin/bash
2:bin:x:1:1:bin:/bin:/sbin/nologin
3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
4:adm:x:3:4:adm:/var/adm:/sbin/nologin
5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6:sync:x:5:0:sync:/sbin:/bin/sync
7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8:halt:x:7:0:halt:/sbin:/sbin/halt
9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:operator:x:11:0:operator:/root:/sbin/nologin
處理單個文件的命令格式
[root@linux-server ~]# sed [options] '[匹配模式] sed 的內(nèi)部命令' file1
例
[root@linux-server ~]# sed -i 's/root/toor/g' test.txt
處理多個文件的命令格式
[root@linux-server ~]# sed [options] '[匹配模式] [sed 的內(nèi)部命令]' file1 file2
例
[root@linux-server ~]# sed -i 's/root/toor/g' test1.txt test2.txt
匹配模式是用于在文件中每一行進行匹配到模式搓侄,模式可以是正則,也可以是文件的行號话速,
sed和grep不一樣讶踪,不管是否找到指定的模式,它的退出狀態(tài)都是0泊交,只有當命令存在語法錯誤時乳讥,sed的退出狀態(tài)才是非0柱查。
三、支持正則表達式
與grep一樣雏婶,sed在文件中查找模式時也可以使用正則表達式(RE)和各種元字符物赶。正則表達式是在斜杠間的模式,用于查找和替換留晚,以下是sed支持的元字符酵紫。
使用基本元字符集^, $, ., *, [], [^], \< \>,\(\),\{\}
使用擴展元字符集?, +, { }, |, ( )
使用擴展元字符的方式:
[root@linux-server ~]# sed -r 's/^root/admin/g' test.txt
在實際使用的時候,都會加上 -r 參數(shù)错维,即使沒有用的擴展正則也不會有任何影響奖地。
四、sed基本用法
打印
sed 默認會輸出文件的每一行赋焕,無論這行內(nèi)容是否能匹配上匹配模式参歹,假如被匹配到的則會再輸出一次。
[root@linux-server ~]# sed -r '' test.txt
[root@linux-server ~]# sed -r 'p' test.txt
p
是 sed 的內(nèi)部命令隆判,是 打尤印(輸出) 的作用
屏蔽默認輸出使用 -n 選項
[root@linux-server ~]# sed -rn 'p' test.txt
[root@linux-server ~]# sed -rn '/root/p' test.txt
顯示root的行
[root@linux-server ~]# sed -rn '/^root/p' test.txt
^root 匹配root開頭
搜索替換
sed會自動打印文件的每一行,同時查找模式匹配的行侨嘀,找到后執(zhí)行后面的命令臭挽,默認是 p 打印(不加 -n 的情況下)
搜索每一行咬腕,找到有 root 的欢峰,把第一個替換為 admin
[root@linux-server ~]# sed -r 's/root/admin/' test.txt
搜索每一行,找到所有的 root 字符涨共,進行全局替換為 `admin`
[root@linux-server ~]# sed -r 's/root/admin/g' test.txt
i 是同時忽略大小寫
[root@linux-server ~]# sed -r 's/root/admin/gi' test.txt
找到含有 root 的進行刪除
[root@linux-server ~]# sed -r '/root/d' test.txt
可以使用不同的 字符 作為界定符號纽帖,注意進行轉(zhuǎn)義
[root@linux-server ~]# sed -r '\#root#d' test.txt
注意:
當在模式匹配中使用其他界定符號時,需要對其進行轉(zhuǎn)義举反。
其他界定符用在 s 搜索替換時不必轉(zhuǎn)義懊直。例如:
[root@linux-server ~]# sed -r 's#root#admin#' test.txt
[root@linux-server ~]# sed -r 's%root%admin%' test.txt
[root@linux-server ~]# sed -r 's|root|admin|' test.txt
五、sed擴展
地址(定址)
地址用于決定對哪些 行 進行編輯火鼻。地址形式可以是數(shù)字吹截、正則表達式或二者的結(jié)合。如果沒有指定地址凝危,sed將處理輸入文件中的所有行波俄。
全部分刪除
[root@linux-server ~]# sed -r 'd' test.txt
第 3 行刪除
[root@linux-server ~]# sed -r '3 d' test.txt
第 1 行到第 3 行都刪除
[root@linux-server ~]# sed -r '1,3 d' test.txt
含有 root 字符串的行刪除
[root@linux-server ~]# sed -r '/root/ d' test.txt
從含有 root 字符串的行開始刪除,總共刪除 5 行蛾默,5 行中包含了 被匹配成功的行
[root@linux-server ~]# sed -r '/root/,5 d' test.txt
從含有 halt 的行開始刪除懦铺,并刪除此行之后的 2 行,就是總共刪除 3 行
[root@linux-server ~]# sed -r '/halt/,+2 d' test.txt
含有 root 的行不刪除支鸡,其他都刪除
[root@linux-server ~]# sed -r '/root/ !d' test.txt
使用行號除以 2 冬念,余數(shù)是 1 的行刪除
[root@linux-server ~]# sed -r '1~2 d' test.txt
使用行號除以 2趁窃, 余數(shù) 是 0 的 打印出來
[root@linux-server ~]# sed -rn '0~2 p' test.txt
試試下面這個, 就是 每次處理的行號是被除數(shù)急前,第二個數(shù)是除數(shù)醒陆,第一數(shù)是 余數(shù)
[root@linux-server ~]# sed -rn '0~3 p' test.txt
六、sed命令
sed命令告訴 sed 對匹配到的行進行何種操作裆针,包括打印刨摩、刪除、修改等世吨。
命令功能詳解
p 打印行
d 刪除行
a 在當前行后添加一行或多行
c 用新文本修改(替換)當前行中的文本
i 在當前行之前插入文本
l 列出非打印字符
n 讀入下一輸入行澡刹,并從下一條命令而不是第一條命令開始對其的處理
! 對所選行以外的所有行應(yīng)用命令
g 全局替換
r 從文件中讀
w 將行寫入文件
------------------------以下為擴展-----------------------
h 清除保持空間的內(nèi)容后,把模式空間里的內(nèi)容復(fù)制到保持空間
H 把模式空間里的內(nèi)容追加到保持空間
g 清除模式空間的內(nèi)容后耘婚, 取出保持空間的內(nèi)容罢浇,并復(fù)制到模式空間
G 取出保持空間的內(nèi)容,追加在模式空間原有內(nèi)容的后面
x 交換模式空間與保持空間的內(nèi)容
sed 部分命令示例 :
替換命令:s
[root@linux-server ~]# sed -rn 's/[0-9][0-9]/&.5/' test.txt //&代表在查找串中匹配到的所有內(nèi)容
[root@linux-server ~]# sed -r 's/(no)login/\1不可登錄/' test.txt
部分輸出為
bin:x:1:1:bin:/bin:/sbin/no不可登錄
讀文件命令:r
在 test.txt 文件的第二行下面沐祷,插入 從 /etc/hosts 文件中讀取到的內(nèi)容
[root@linux-server ~]# sed -r '2r /etc/hosts' test.txt
在 test.txt 文件中含有字符 2 下面嚷闭,插入 從 /etc/hosts 文件中讀取到的內(nèi)容
[root@linux-server ~]# sed -r '/2/r /etc/hosts' test.txt
寫文件命令:w
把 test.txt 文件中含有 字符 root 的行寫入到文件 newfile, newfile 文件不存在則創(chuàng)建,存在則覆蓋
[root@linux-server ~]# sed -r '/root/w newfile' test.txt
把 test.txt 文件中的第 3 行到最后的內(nèi)容寫入到文件 /new1.txt 中
[root@linux-server ~]# sed -r '3,$w /new1.txt' test.txt
追加命令:a
[root@linux-server ~]# sed -r '$a 1.1.1.1 www.baidu.com' /etc/hosts
插入命令:i
[root@linux-server ~]# sed -r '2i\1111111111111' /etc/hosts
[root@linux-server ~]# sed -r '2i111111111\
> 2222222222\
> 3333333333' /etc/hosts
修改命令:c
[root@linux-server ~]# sed -r '2c\1111111111111' /etc/hosts
[root@linux-server ~]# sed -r '2c\111111111111\
> 22222222222\
> 33333333333' /etc/hosts
獲取下一行命令:n
[root@linux-server ~]# sed -r '/eastern/{ n; d }' datafile
[root@linux-server ~]# sed -r '/eastern/{ n; s/AM/Archile/ }' datafile
暫存和取用命令:h H g G
[root@linux-server ~]# sed -r '1h;$G' /etc/hosts
[root@linux-server ~]# sed -r '1{h;d};$G' /etc/hosts
[root@linux-server ~]# sed -r '1h; 2,$g' /etc/hosts
[root@linux-server ~]# sed -r '1h; 2,3H; $G' /etc/hosts
暫存空間和模式空間互換命令:x
[root@linux-server ~]# sed -r '4h; 5x; 6G' /etc/hosts
反向選擇: !
[root@linux-server ~]# sed -r '3d' /etc/hosts
[root@linux-server ~]# sed -r '3!d' /etc/hosts
七赖临、關(guān)于選項
選項 功能
-e 允許多項編輯
-f 指定sed腳本文件名
-n 取消默認的輸出
-i inplace凌受,就地編輯
-r 支持擴展元字符
多重編輯選項:-e
[root@linux-server ~]# sed -e '1,3 d' -e 's/root/admin/' test.txt
4:adm:x:3:4:adm:/var/adm:/sbin/nologin
5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6:sync:x:5:0:sync:/sbin:/bin/sync
7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8:halt:x:7:0:halt:/sbin:/sbin/halt
9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:operator:x:11:0:operator:/admin:/sbin/nologin
[root@linux-server ~]#
八、sed常見操作舉例
刪除配置文件中 # 號注釋的行
[root@linux-server ~]# sed -ri '/^#/d' file.conf
刪除開頭的一個或者多個 空格或者 Tab 鍵
[root@linux-server ~]# sed -ri '/^[ \t]*#/d' file.conf
YUM 源修改
[root@linux-server ~]# sed -ri s/^#baseurl/baseurl/g /etc/yum.repos.d/CentOS-Base.repo
[root@linux-server ~]# sed -ri s/^mirrorlist/#mirrorlist/g /etc/yum.repos.d/CentOSBase.repo
空格和table鍵 '/^#/d' [ \t] * 空格和table
刪除配置文件中//號注釋行
[root@linux-server ~]# sed -ri '\Y^[ \t]*//Yd' file.conf
刪除無內(nèi)容空行
- 開頭和結(jié)尾之間什么都沒有的行
- 開頭和結(jié)尾之間有多個空格的行
- 開頭和結(jié)尾之間有多個 Tab 鍵的行
[root@linux-server ~]# sed -ri '/^[ \t]*$/d' file.conf
刪除注釋行及空行:
以下 3 中效果一樣思杯,挑一個自己喜歡的
[root@linux-server ~]# sed -ri '/^[ \t]*#/d; /^[ \t]*$/d' /etc/vsftpd/vsftpd.conf
[root@linux-server ~]# sed -ri '/^[ \t]*#|^[ \t]*$/d' /etc/vsftpd/vsftpd.conf
[root@linux-server ~]# sed -ri '/^[ \t]*($|#)/d' /etc/vsftpd/vsftpd.conf
修改文件:
[root@linux-server ~]# sed -ri '$a\chroot_local_user=YES' /etc/vsftpd/vsftpd.conf
[root@linux-server ~]# sed -ri '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config
[root@linux-server ~]# sed -ri '/UseDNS/cUseDNS no' /etc/ssh/sshd_config
[root@linux-server ~]# sed -ri '/GSSAPIAuthentication/cGSSAPIAuthentication no' /etc/ssh/sshd_config
給文件行添加注釋:
[root@linux-server ~]# sed -r '2,6s/^/#/' a.txt
使用小括號進行分組,可以有多個分組挠进, 后面可以使用 \1 獲取到第一個分組的內(nèi)容
[root@linux-server ~]# sed -r '2,6s/(.*)/#\1/' a.txt
[root@linux-server ~]# sed -r '2,6s/.*/#&/' a.txt &匹配前面查找的內(nèi)容
[root@linux-server ~]# sed -r '3,$ s/^#*/#/' a.txt 將行首零個或多個#換成一個#
[root@linux-server ~]# sed -r '30,50s/^[ \t]*#*/#/' /etc/nginx.conf
[root@linux-server ~]# sed -r '2,8s/^[ \t#]*/#/' /etc/nginx.conf
sed中使用外部變量
[root@linux-server ~]# var1=11111
# 無效
[root@linux-server ~]# sed -r '3a$var1' /etc/hosts
# 正確
[root@linux-server ~]# sed -r "3a$var1" /etc/hosts
# 有效
[root@linux-server ~]# sed -r 3a$var1 /etc/hosts
# 報錯
[root@linux-server ~]# sed -r "$a$var1" /etc/hosts
# 有效色乾,但是中間不能有空格
[root@linux-server ~]# sed -r '$a'"$var1" /etc/hosts
# 有效, 將第一個 $ 進行轉(zhuǎn)義
[root@linux-server ~]# sed -r "\$a $var1" /etc/hosts