# -n選項表示只過濾出匹配的行,搜索字符後面的p表示打印出結果 [root@centos01 test_sed_dir]# sed -n '/root/'p sed_test.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@centos01 test_sed_dir]# sed -n '/o\+t/'p sed_test.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@centos01 test_sed_dir]# sed -nr '/o+t/'p sed_test.txt # -r選項表示不須要對搜索字符串進行轉義 root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@centos01 test_sed_dir]# cat -n sed_test.txt |head -n 5 1 root:x:0:0:root:/root:/bin/bash 2 tooost 3 ooooost 4 bin:x:1:1:bin:/bin:/sbin/nologin 5 daemon:x:2:2:daemon:/sbin:/sbin/nologin # 打印第2行 [root@centos01 test_sed_dir]# sed -n '2'p sed_test.txt tooost # 1,5 打印1-5行 [root@centos01 test_sed_dir]# sed -nr '1,5'p sed_test.txt root:x:0:0:root:/root:/bin/bash tooost ooooost bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin # 25行以後的全部行 [root@centos01 test_sed_dir]# sed -n '25,$'p sed_test.txt test03:x:1003:1004::/home/test03:/bin/bash test06:x:1004:1005::/home/test06:/bin/bash test07:x:1005:1006::/home/test07:/bin/bash test08:x:1006:1007::/home/test08:/bin/bash # 多個搜索串 [root@centos01 test_sed_dir]# sed -nr '/root|opera/'p sed_test.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin #-e 多個過濾匹配 [root@centos01 test_sed_dir]# sed -e '1,5'p -e'25,$'p -n sed_test.txt root:x:0:0:root:/root:/bin/bash tooost ooooost bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin test03:x:1003:1004::/home/test03:/bin/bash test06:x:1004:1005::/home/test06:/bin/bash test07:x:1005:1006::/home/test07:/bin/bash test08:x:1006:1007::/home/test08:/bin/bash # 搜索串後加I 表示不區分大小寫 [root@centos01 test_sed_dir]# sed -nr '/ROOT/'Ip sed_test.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin # 搜索串後加d 表示刪除,只列出剩餘的行,這時並無刪除文件內容 [root@centos01 test_sed_dir]# wc -l sed_test.txt 28 sed_test.txt [root@centos01 test_sed_dir]# tail -n 2 sed_test.txt test07:x:1005:1006::/home/test07:/bin/bash test08:x:1006:1007::/home/test08:/bin/bash [root@centos01 test_sed_dir]# sed '1,26'd sed_test.txt test07:x:1005:1006::/home/test07:/bin/bash test08:x:1006:1007::/home/test08:/bin/bash # 搜索串後加d,加-i選項,就是針對文件內容進行更改 [root@centos01 test_sed_dir]# sed -i '27,28'd sed_test.txt [root@centos01 test_sed_dir]# tail -n 2 sed_test.txt test03:x:1003:1004::/home/test03:/bin/bash test06:x:1004:1005::/home/test06:/bin/bash [root@centos01 test_sed_dir]# wc -l sed_test.txt 26 sed_test.txt [root@centos01 test_sed_dir]# sed -i '/test02/'d sed_test.txt [root@centos01 test_sed_dir]# sed -n '/test02/'p sed_test.txt # 查找替換,搜索串末尾加s/源串/目的串/g',其中g表明更改查到行的全部匹配串 [root@centos01 test_sed_dir]# sed -n '1,10s/root/rt/g'p sed_test.txt rt:x:0:0:rt:/rt:/bin/bash # 查找使用正則 [root@centos01 test_sed_dir]# sed -nr '1,10s/ro+t/rt/g'p sed_test.txt rt:x:0:0:rt:/rt:/bin/bash # 使用正則交換用:分隔後的第一段和最後一段,其中\3\2\1 分別表示匹配到的第3項、第2項、第1項 [root@centos01 test_sed_dir]# head sed_test.txt root:x:0:0:root:/root:/bin/bash tooost ooooost 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 sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt [root@centos01 test_sed_dir]# head sed_test.txt | sed -r 's/([^:]+):(.*):([^:]+) /\3:\2:\1/' /bin/bash:x:0:0:root:/root:root tooost ooooost /sbin/nologin:x:1:1:bin:/bin:bin /sbin/nologin:x:2:2:daemon:/sbin:daemon /sbin/nologin:x:3:4:adm:/var/adm:adm /sbin/nologin:x:4:7:lp:/var/spool/lpd:lp /bin/sync:x:5:0:sync:/sbin:sync /sbin/shutdown:x:6:0:shutdown:/sbin:shutdown /sbin/halt:x:7:0:halt:/sbin:halt # 把/root進行替換,能夠進行轉義或者使用別的符號(@、#等)進行劃分 [root@centos01 test_sed_dir]# head sed_test.txt | sed -r 's#/root#~root#'g root:x:0:0:root:~root:/bin/bash tooost ooooost 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 sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt # 刪除全部的字母 [root@centos01 test_sed_dir]# head sed_test.txt | sed -r 's/[a-zA-Z]//'g ::0:0::/:// ::1:1::/:// ::2:2::/:// ::3:4:://:// ::4:7::///:// ::5:0::/:// ::6:0::/:// ::7:0::/:// # 在每一行前加一個字符,例,加***: [root@centos01 test_sed_dir]# head sed_test.txt | sed -r 's/(.*)/***:&/' ***:root:x:0:0:root:/root:/bin/bash ***:tooost ***:ooooost ***: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 ***:sync:x:5:0:sync:/sbin:/bin/sync ***:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown ***:halt:x:7:0:halt:/sbin:/sbin/halt