網站首頁 編程語言 正文
sed的作用
sed是Stream Editor(流編輯器)的縮寫,簡稱流編輯器;用來處理文件的
sed是一行一行讀取文件內容并按照要求進行處理,把處理后的結果輸出到屏幕
- 首先sed讀取文件中的一行內容,把其保存在一個臨時緩存區中(也稱為模式空間)
- 然后根據需求處理臨時緩沖區中的行,完成后把該行發送到屏幕上
總結:
- 由于sed把每一行都存在臨時緩沖區中,對這個副本進行編輯,所以不會直接修改原文件
- Sed主要用來自動編輯一個或多個文件;簡化對文件的反復操作,對文件進行過濾和轉換操作
sed使用方法介紹
sed常見的語法格式有兩種,一種叫命令行模式,另一種叫腳本模式。
命令行格式
sed [options] ‘處理動作’ 文件名
- 常用選項
選項 | 說明 | 備注 |
---|---|---|
-e | 進行多項(多次)編輯 | 可以編輯多個匹配參數 |
-n | 取消默認輸出 | 不自動打印模式空間 |
-r | 使用擴展正則表達式 | ? |
-i | 原地編輯 | 會修改源文件 |
-f | 指定sed腳本的文件名 | ? |
- 常見處理動作
以下所有的動作都要在單引號里
動作 | 說明 | 備注 |
---|---|---|
‘p’ | 打印 | ? |
‘i’ 在指定行之前插入內容 | 類似vim里的大寫O | ? |
‘a’ | 在指定行之后插入內容 | 類似vim里的小寫o |
‘c’ | 替換指定行所有內容 | 很常用 |
‘d’ | 刪除指定行 | ? |
舉例說明
文件準備 vim a.txt root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 298374837483 172.16.0.254 10.1.1.1
對文件進行增、刪、改、查操作
打印文件內容
sed '' a.txt 對文件什么都不做 sed -n 'p' a.txt 打印每一行,并取消默認輸出 sed -n '1p' a.txt 打印第1行 sed -n '2p' a.txt 打印第2行 sed -n '1,5p' a.txt 打印1到5行 sed -n '$p' a.txt 打印最后1行
增加文件內容
i
地址定位的上面插入
a
下面插入
sed '$a99999' a.txt 文件最后一行下面增加內容 sed 'a99999' a.txt 文件每行下面增加內容 sed '5a99999' a.txt 文件第5行下面增加內容 sed '$i99999' a.txt 文件最后一行上一行增加內容 sed 'i99999' a.txt 文件每行上一行增加內容 sed '6i99999' a.txt 文件第6行上一行增加內容 sed '/^adm/ihello' 以adm開頭行的上一行插入內容
c
替換指定的整行內容
sed '5chello world' a.txt 替換文件第5行內容 sed 'chello world' a.txt 替換文件所有內容 sed '1,5chello world' a.txt 替換文件1到5號內容為hello world sed '/^daemon/c888888' a.txt 替換以user01開頭的行
刪除文件內容
sed '1d' a.txt 刪除文件第1行 sed '1,5d' a.txt 刪除文件1到5行 sed '$d' a.txt 刪除文件最后一行
對文件進行搜索替換操作
語法:sed 選項 ‘s/搜索的內容/替換的內容/動作’ 需要處理的文件
其中,s表示search搜索;斜杠==/表示分隔符,可以自己定義;動作一般是打印p和全局替換g==(默認只替換每行第一個匹配到的內容)
sed -n 's/root/ROOT/p' 1.txt sed -n 's/root/ROOT/gp' 1.txt sed -n 's/^#//gp' 1.txt sed -n 's@/sbin/nologin@itcast@gp' a.txt sed -n 's/\/sbin\/nologin/itcast/gp' a.txt sed -n '10s#/sbin/nologin#itcast#p' a.txt uucp:x:10:14:uucp:/var/spool/uucp:itcast sed -n 's@/sbin/nologin@itcastheima@p' 2.txt 注意:搜索替換中的分隔符可以自己指定 sed -n '1,5s/^/#/p' a.txt 注釋掉文件的1-5行內容 #root:x:0:0:root:/root:/bin/bash #bin:x:1:1:bin:/bin:/sbin/nologin #daemon:x:2:2:daemon:/sbin:/sbin/nologin #adm:x:3:4:adm:/var/adm:/sbin/nologin #lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
其他命令
命令 | 解釋 | 備注 |
---|---|---|
r | 從另外文件中讀取內容 | ? |
w | 內容另存為 | ? |
& | 保存查找串以便在替換串中引用 | 和()相同 |
= | 打印行號 | ? |
! | 對所選行以外的所有行應用命令,放到行數之后 | ‘1,5!’ |
q | 退出 | ? |
舉例說明:
r 從文件中讀取輸入行 w 將所選的行寫入文件 sed '3r /etc/hosts' 2.txt sed '$r /etc/hosts' 2.txt sed '/root/w a.txt' 2.txt sed '/[0-9]{4}/w a.txt' 2.txt sed -r '/([0-9]{1,3}\.){3}[0-9]{1,3}/w b.txt' 2.txt ! 對所選行以外的所有行應用命令,放到行數之后 sed -n '1!p' 1.txt sed -n '4p' 1.txt sed -n '4!p' 1.txt cat -n 1.txt sed -n '1,17p' 1.txt sed -n '1,17!p' 1.txt & 保存查找串以便在替換串中引用 \(\) sed -n '/root/p' a.txt root:x:0:0:root:/root:/bin/bash sed -n 's/root/#&/p' a.txt #root:x:0:0:root:/root:/bin/bash sed -n 's/^root/#&/p' passwd 注釋掉以root開頭的行 sed -n -r 's/^root|^stu/#&/p' /etc/passwd 注釋掉以root開頭或者以stu開頭的行 sed -n '1,5s/^[a-z].*/#&/p' passwd 注釋掉1~5行中以任意小寫字母開頭的行 sed -n '1,5s/^/#/p' /etc/passwd 注釋1~5行 或者 sed -n '1,5s/^/#/p' passwd 以空開頭的加上# sed -n '1,5s/^#//p' passwd 以#開頭的替換成空 sed -n '/^root/p' 1.txt sed -n 's/^root/#&/p' 1.txt sed -n 's/\(^root\)/#\1/p' 1.txt sed -nr '/^root|^stu/p' 1.txt sed -nr 's/^root|^stu/#&/p' 1.txt = 打印行號 sed -n '/bash$/=' passwd 打印以bash結尾的行的行號 sed -ne '/root/=' -ne '/root/p' passwd sed -n '/nologin$/=;/nologin$/p' 1.txt sed -ne '/nologin$/=' -ne '/nologin$/p' 1.txt q 退出 sed '5q' 1.txt sed '/mail/q' 1.txt sed -r '/^yunwei|^mail/q' 1.txt sed -n '/bash$/p;10q' 1.txt ROOT:x:0:0:root:/root:/bin/bash 綜合運用: sed -n '1,5s/^/#&/p' 1.txt #root:x:0:0:root:/root:/bin/bash #bin:x:1:1:bin:/bin:/sbin/nologin #daemon:x:2:2:daemon:/sbin:/sbin/nologin #adm:x:3:4:adm:/var/adm:/sbin/nologin #lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sed -n '1,5s/\(^\)/#\1/p' 1.txt #root:x:0:0:root:/root:/bin/bash #bin:x:1:1:bin:/bin:/sbin/nologin #daemon:x:2:2:daemon:/sbin:/sbin/nologin #adm:x:3:4:adm:/var/adm:/sbin/nologin #lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
其他選項
-e 多項編輯 -r 擴展正則 -i 修改原文件 sed -ne '/root/p' 1.txt -ne '/root/=' root:x:0:0:root:/root:/bin/bash 1 sed -ne '/root/=' -ne '/root/p' 1.txt 1 root:x:0:0:root:/root:/bin/bash 在1.txt文件中的第5行的前面插入“hello world”;在1.txt文件的第8行下面插入“貓貓貓” sed -e '5ihello world' -e '8a貓貓貓' 1.txt -e '5=;8=' sed -n '1,5p' 1.txt sed -ne '1p' -ne '5p' 1.txt sed -ne '1p;5p' 1.txt 過濾vsftpd.conf文件中以#開頭和空行: grep -Ev '^#|^$' /etc/vsftpd/vsftpd.conf sed -e '/^#/d' -e '/^$/d' /etc/vsftpd/vsftpd.conf [sed '/^#/d;/^$/d' /etc/vsftpd/vsftpd.conf sed -r '/^#|^$/d' /etc/vsftpd/vsftpd.conf 過濾smb.conf文件中生效的行: sed -e '/^#/d' -e '/^;/d' -e '/^$/d' -e '/^\t$/d' -e '/^\t#/d' smb.conf sed -r '/^(#|$|;|\t#|\t$)/d' smb.conf sed -e '/^#/d' -e '/^;/d' -e '/^$/d' -e '/^\t$/d' -e '/^\t#/' smb.conf grep '^[^a-z]' 1.txt sed -n '/^[^a-z]/p' 1.txt 過濾出文件中的IP地址: grep -E '([0-9]{1,3}\.){3}[0-9]{1,3}' 1.txt 192.168.0.254 sed -nr '/([0-9]{1,3}\.){3}[0-9]{1,3}/p' 1.txt 192.168.0.254 grep -o -E '([0-9]{1,3}\.){3}[0-9]{1,3}' 2.txt 10.1.1.1 10.1.1.255 255.255.255.0 sed -nr '/([0-9]{1,3}\.){3}[0-9]{1,3}/p' 2.txt 10.1.1.1 10.1.1.255 255.255.255.0 過濾出ifcfg-eth0文件中的IP、子網掩碼、廣播地址 grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' ifcfg-eth0 10.1.1.1 255.255.255.0 10.1.1.254 sed -nr '/([0-9]{1,3}\.){3}[0-9]{1,3}/p' ifcfg-eth0|cut -d'=' -f2 10.1.1.1 255.255.255.0 10.1.1.254 sed -nr '/([0-9]{1,3}\.){3}[0-9]{1,3}/p' ifcfg-eth0|sed -n 's/[A-Z=]//gp' 10.1.1.1 255.255.255.0 10.1.1.254 ifconfig eth0|sed -n '2p'|sed -n 's/[:a-Z]//gp'|sed -n 's/ /\n/gp'|sed '/^$/d' 10.1.1.1 10.1.1.255 255.255.255.0 ifconfig | sed -nr '/([0-9]{1,3}\.)[0-9]{1,3}/p' | head -1|sed -r 's/([a-z:]|[A-Z/t])//g'|sed 's/ /\n/g'|sed '/^$/d' ifconfig eth0|sed -n '2p'|sed -n 's/.*addr:\(.*\) Bcast:\(.*\) Mask:\(.*\)/\1\n\2\n\3/p' 10.1.1.1 10.1.1.255 255.255.255.0 -i 選項 直接修改原文件 sed -i 's/root/ROOT/;s/stu/STU/' 11.txt sed -i '17{s/YUNWEI/yunwei/;s#/bin/bash#/sbin/nologin#}' 1.txt sed -i '1,5s/^/#&/' a.txt 注意: -ni 不要一起使用 p命令 不要再使用-i時使用
sed結合正則使用
sed 選項 ‘sed命令或者正則表達式或者地址定位’ 文件名
定址用于決定對哪些行進行編輯。地址的形式可以是數字、正則表達式、或二者的結合。
如果沒有指定地址,sed將處理輸入文件的所有行。
正則 | 說明 | 備注 |
---|---|---|
/key/ | 查詢包含關鍵字的行 | sed -n ‘/root/p’ 1.txt |
/key1/,/key2/ | 匹配包含兩個關鍵字之間的行 | sed -n ‘/^adm/,/^mysql/p’ 1.txt |
/key/,x | 從匹配關鍵字的行開始到文件第x行之間的行(包含關鍵字所在行) | sed -n ‘/^ftp/,7p’ |
x,/key/ | 從文件的第x行開始到與關鍵字的匹配行之間的行 | ? |
x,y! | 不包含x到y行 | ? |
/key/! | 不包括關鍵字的行 | sed -n ‘/bash$/!p’ 1.txt |
補充擴展總結
1、正則表達式必須以”/“前后規范間隔 例如:sed '/root/d' file 例如:sed '/^root/d' file 2、如果匹配的是擴展正則表達式,需要使用-r選來擴展sed grep -E sed -r + ? () {n,m} | \d 注意: 在正則表達式中如果出現特殊字符(^$.*/[]),需要以前導 "\" 號做轉義 eg:sed '/\$foo/p' file 3、逗號分隔符 例如:sed '5,7d' file 刪除5到7行 例如:sed '/root/,/ftp/d' file 刪除第一個匹配字符串"root"到第一個匹配字符串"ftp"的所有行本行不找 循環執行 4、組合方式 例如:sed '1,/foo/d' file 刪除第一行到第一個匹配字符串"foo"的所有行 例如:sed '/foo/,+4d' file 刪除從匹配字符串”foo“開始到其后四行為止的行 例如:sed '/foo/,~3d' file 刪除從匹配字符串”foo“開始刪除到3的倍數行(文件中) 例如:sed '1~5d' file 從第一行開始刪每五行刪除一行 例如:sed -nr '/foo|bar/p' file 顯示配置字符串"foo"或"bar"的行 例如:sed -n '/foo/,/bar/p' file 顯示匹配從foo到bar的行 例如:sed '1~2d' file 刪除奇數行 例如:sed '0-2d' file 刪除偶數行 sed '1~2!d' file 5、特殊情況 例如:sed '$d' file 刪除最后一行 例如:sed '1d' file 刪除第一行 6、其他: sed 's/.//' a.txt 刪除每一行中的第一個字符 sed 's/.//2' a.txt 刪除每一行中的第二個字符 sed 's/.//N' a.txt 從文件中第N行開始,刪除每行中第N個字符(N>2) sed 's/.$//' a.txt 刪除每一行中的最后一個字符
總結
原文鏈接:https://blog.csdn.net/Cantevenl/article/details/115448029
相關推薦
- 2022-10-10 redis緩存延時雙刪的原因分析_Redis
- 2022-11-11 react中代碼塊輸出,代碼高亮顯示,帶行號,能復制的問題_React
- 2022-03-20 C語言數據結構之鏈隊列的基本操作_C 語言
- 2022-09-18 Redis6?主從復制及哨兵機制的實現_Redis
- 2022-10-20 Android開發使用RecyclerView添加點擊事件實例詳解_Android
- 2023-01-10 阿里云服務器?jdk1.8?安裝配置教程_服務器其它
- 2022-05-22 Python學習之os包使用教程詳解_python
- 2022-08-19 注解配置SpringMVC
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支