shell编程之文本处理工具sed
shell編程之文本處理工具sed
文章目錄
- shell編程之文本處理工具sed
- 一、文件編輯器知多少
- 二、強悍的sed介紹
- 1. sed用來做啥?
- 2. sed如何處理文件?
- 三、sed使用方法介紹
- 1. 命令行格式
- ㈠ 語法格式
- ㈡ 舉例說明
- ① 對文件進行==增、刪、改、查==操作
- 1)打印文件內容
- 2)增加文件內容
- 3)修改文件內容
- 4)刪除文件內容
- ② ==對文件進行搜索替換操作==
- ③ 其他命令
- ④ 其他選項
- ⑤ ==sed結合正則使用==
- 2. 腳本格式
- ㈠ 用法
- ㈡ 注意事項
- ㈢舉例說明
- 3. 補充擴展總結
- 四、練習
- 五、實戰
一、文件編輯器知多少
Linux下:
? vim 、 vi 、gedit、 nano、 emacs
二、強悍的sed介紹
1. sed用來做啥?
sed是Stream Editor(流編輯器)的縮寫,簡稱流編輯器;用來處理文件的。
2. sed如何處理文件?
sed是一行一行讀取文件內容并按照要求進行處理,把處理后的結果輸出到屏幕。
總結:
三、sed使用方法介紹
sed常見的語法格式有兩種,一種叫命令行模式,另一種叫腳本模式。
1. 命令行格式
㈠ 語法格式
sed [options] 處理動作 文件名- 常用選項
| -e | 進行多項(多次)編輯 | |
| -n | 取消默認輸出 | 不自動打印模式空間 |
| -r | 使用擴展正則表達式 | |
| -i | 原地編輯(修改源文件) | |
| -f | 指定sed腳本的文件名 |
- 常見處理動作
丑話說在前面:以下所有的動作都要在單引號里,你敢出軌,回家跪搓衣板
| ‘p’ | 打印 | |
| ‘i’ | 在指定行之前插入內容 | 類似vim里的大寫O |
| ‘a’ | 在指定行之后插入內容 | 類似vim里的小寫o |
| ‘c’ | 替換指定行所有內容 | |
| ‘d’ | 刪除指定行 |
㈡ 舉例說明
- 文件準備
① 對文件進行增、刪、改、查操作
語法:sed 選項 '定位+命令’ 需要處理的文件
1)打印文件內容
[root@server ~]# sed '' a.txt 對文件什么都不做 [root@server ~]# sed -n 'p' a.txt 打印每一行,并取消默認輸出 [root@server ~]# sed -n '1p' a.txt 打印第1行 [root@server ~]# sed -n '2p' a.txt 打印第2行 [root@server ~]# sed -n '1,5p' a.txt 打印1到5行 [root@server ~]# sed -n '$p' a.txt 打印最后1行2)增加文件內容
i 地址定位的上面插入
a 下面插入
[root@server ~]# sed '$a99999' a.txt 文件最后一行下面增加內容 [root@server ~]# sed 'a99999' a.txt 文件每行下面增加內容 [root@server ~]# sed '5a99999' a.txt 文件第5行下面增加內容 [root@server ~]# sed '$i99999' a.txt 文件最后一行上一行增加內容 [root@server ~]# sed 'i99999' a.txt 文件每行上一行增加內容 [root@server ~]# sed '6i99999' a.txt 文件第6行上一行增加內容 [root@server ~]# sed '/^uucp/ihello' 以uucp開頭行的上一行插入內容3)修改文件內容
c 替換指定的整行內容
[root@server ~]# sed '5chello world' a.txt 替換文件第5行內容 [root@server ~]# sed 'chello world' a.txt 替換文件所有內容 [root@server ~]# sed '1,5chello world' a.txt 替換文件1到5號內容為hello world [root@server ~]# sed '/^user01/c888888' a.txt 替換以user01開頭的行4)刪除文件內容
[root@server ~]# sed '1d' a.txt 刪除文件第1行 [root@server ~]# sed '1,5d' a.txt 刪除文件1到5行 [root@server ~]# sed '$d' a.txt 刪除文件最后一行② 對文件進行搜索替換操作
語法:sed 選項 ‘s/搜索的內容/替換的內容/動作’ 需要處理的文件
其中,s表示search搜索;斜杠/表示分隔符,可以自己定義;動作一般是打印p和全局替換g
[root@server ~]# sed -n 's/root/ROOT/p' 1.txt [root@server ~]# sed -n 's/root/ROOT/gp' 1.txt [root@server ~]# sed -n 's/^#//gp' 1.txt [root@server ~]# sed -n 's@/sbin/nologin@itcast@gp' a.txt [root@server ~]# sed -n 's/\/sbin\/nologin/itcast/gp' a.txt [root@server ~]# sed -n '10s#/sbin/nologin#itcast#p' a.txt uucp:x:10:14:uucp:/var/spool/uucp:itcast [root@server ~]# sed -n 's@/sbin/nologin@itcastheima@p' 2.txt 注意:搜索替換中的分隔符可以自己指定[root@server ~]# 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 將所選的行寫入文件 [root@server ~]# sed '3r /etc/hosts' 2.txt [root@server ~]# sed '$r /etc/hosts' 2.txt [root@server ~]# sed '/root/w a.txt' 2.txt [root@server ~]# sed '/[0-9]{4}/w a.txt' 2.txt [root@server ~]# sed -r '/([0-9]{1,3}\.){3}[0-9]{1,3}/w b.txt' 2.txt! 對所選行以外的所有行應用命令,放到行數之后 [root@server ~]# sed -n '1!p' 1.txt [root@server ~]# sed -n '4p' 1.txt [root@server ~]# sed -n '4!p' 1.txt [root@server ~]# cat -n 1.txt [root@server ~]# sed -n '1,17p' 1.txt [root@server ~]# sed -n '1,17!p' 1.txt & 保存查找串以便在替換串中引用 \(\)[root@server ~]# sed -n '/root/p' a.txt root:x:0:0:root:/root:/bin/bash [root@server ~]# 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 以#開頭的替換成空[root@server ~]# sed -n '/^root/p' 1.txt [root@server ~]# sed -n 's/^root/#&/p' 1.txt [root@server ~]# sed -n 's/\(^root\)/#\1/p' 1.txt [root@server ~]# sed -nr '/^root|^stu/p' 1.txt [root@server ~]# 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.txtq 退出 # sed '5q' 1.txt # sed '/mail/q' 1.txt # sed -r '/^yunwei|^mail/q' 1.txt [root@server ~]# sed -n '/bash$/p;10q' 1.txt ROOT:x:0:0:root:/root:/bin/bash綜合運用: [root@server ~]# 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[root@server ~]# 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 修改原文件[root@server ~]# sed -ne '/root/p' 1.txt -ne '/root/=' root:x:0:0:root:/root:/bin/bash 1 [root@server ~]# 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行下面插入“哈哈哈哈”[root@server ~]# 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文件中以#開頭和空行: [root@server ~]# grep -Ev '^#|^$' /etc/vsftpd/vsftpd.conf [root@server ~]# sed -e '/^#/d' -e '/^$/d' /etc/vsftpd/vsftpd.conf [root@server ~]# sed '/^#/d;/^$/d' /etc/vsftpd/vsftpd.conf [root@server ~]# 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[root@server ~]# grep '^[^a-z]' 1.txt[root@server ~]# sed -n '/^[^a-z]/p' 1.txt過濾出文件中的IP地址: [root@server ~]# grep -E '([0-9]{1,3}\.){3}[0-9]{1,3}' 1.txt 192.168.0.254 [root@server ~]# sed -nr '/([0-9]{1,3}\.){3}[0-9]{1,3}/p' 1.txt 192.168.0.254[root@server ~]# 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[root@server ~]# 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、子網掩碼、廣播地址 [root@server shell06]# 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 [root@server shell06]# 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 [root@server shell06]# 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[root@server shell06]# 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 [root@server shell06]# 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'[root@server shell06]# 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命令或者正則表達式或者地址定位==’== 文件名
| /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 |
2. 腳本格式
㈠ 用法
# sed -f scripts.sh file //使用腳本處理文件 建議使用 ./sed.sh file腳本的第一行寫上 #!/bin/sed -f 1,5d s/root/hello/g 3i777 5i888 a999 p㈡ 注意事項
1) 腳本文件是一個sed的命令行清單。'commands' 2) 在每行的末尾不能有任何空格、制表符(tab)或其它文本。 3) 如果在一行中有多個命令,應該用分號分隔。 4) 不需要且不可用引號保護命令 5) #號開頭的行為注釋㈢舉例說明
# cat passwd stu3:x:509:512::/home/user3:/bin/bash stu4:x:510:513::/home/user4:/bin/bash stu5:x:511:514::/home/user5:/bin/bash# cat sed.sh #!/bin/sed -f 2a\ ****************** 2,$s/stu/user/ $a\ we inster new line s/^[a-z].*/#&/[root@server ~]# cat 1.sed #!/bin/sed -f 3a********************** $chelloworld 1,3s/^/#&/[root@server ~]# sed -f 1.sed -i 11.txt [root@server ~]# cat 11.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 helloworld3. 補充擴展總結
1、正則表達式必須以”/“前后規范間隔 例如:sed '/root/d' file 例如:sed '/^root/d' file2、如果匹配的是擴展正則表達式,需要使用-r選來擴展sed grep -E sed -r + ? () {n,m} | \d注意: 在正則表達式中如果出現特殊字符(^$.*/[]),需要以前導 "\" 號做轉義 eg:sed '/\$foo/p' file3、逗號分隔符 例如: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' file5、特殊情況 例如: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 刪除每一行中的最后一個字符[root@server ~]# cat 2.txt 1 a 2 b 3 c 4 d 5 e 6 f 7 u 8 k 9 o [root@server ~]# sed '/c/,~2d' 2.txt 1 a 2 b 5 e 6 f 7 u 8 k 9 o四、練習
五、實戰
1、寫一個初始化系統的腳本
1)自動修改主機名(如:ip是192.168.0.88,則主機名改為server88.itcast.cc)
a. 更改文件非交互式 sed
/etc/sysconfig/network
b.將本主機的IP截取出來賦值給一個變量ip;再然后將ip變量里以.分割的最后一位賦值給另一個變量ip1
2)自動配置可用的yum源
3)自動關閉防火墻和selinux
2、寫一個搭建ftp服務的腳本,要求如下:
1)不支持本地用戶登錄 local_enable=NO
2) 匿名用戶可以上傳 新建 刪除 anon_upload_enable=YES anon_mkdir_write_enable=YES
3) 匿名用戶限速500KBps anon_max_rate=500000
總結
以上是生活随笔為你收集整理的shell编程之文本处理工具sed的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: shell编程之正则表达式
- 下一篇: shell编程之文本处理工具awk