sed -n 'num1p' file 选出行号为num1的行
sed -n 'num1,num2p' file 选出num1~num2行sed -n 'num1,$p' file 选出num1行到文件末尾grep -n '.*' file |sed 'num1p' -n 带行号的显示sed '/root/p' -n pp.txt 查找带root的行 / /之间和grep 一样 有些正则 需加 -r 参数 类似 grep的 -E-e 多个表达式 或的关系sed -e '/pattern1/p' -e '/pattern2/p' -n或者sed '/pattern1/p;/pattern2/p' -nsed '1,5d' 筛选除了1,5行的cat -n pp.txt |sed -r '/mysql|root/d' 筛选出不含有 mysql或者 root的 行sed 's/str1/str2/' file 在file里把 str1替换成str2 查找显示sed 'num1,num2s/str1/str2/' file 在file的num1~num2把 str替换成str2大小写转换 sed 's/[a-z]/\u&/g' 1.txt u ->upper sed 's/[A-Z]/\l&/g' 1.txt l ->lower -i 修改了重定向至文件[root@bogon ~]# cat -n pp.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 11 games:x:12:100:games:/usr/games:/sbin/nologin 12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 13 nobody:x:99:99:Nobody:/:/sbin/nologin 14 avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin 15 dbus:x:81:81:System message bus:/:/sbin/nologin 16 polkitd:x:999:998:User for polkitd:/:/sbin/nologin 17 abrt:x:173:173::/etc/abrt:/sbin/nologin 18 sssd:x:998:997:User for sssd:/:/sbin/nologin 19 colord:x:997:995:User for colord:/var/lib/colord:/sbin/nologin 20 ntp:x:38:38::/etc/ntp:/sbin/nologin
sed '2p' pp.txt
[root@bogon ~]# sed '2p' pp.txt root:x:0:0:root:/root:/bin/bash#bin:x:1:1:bin:/bin:/sbin/nologin#bin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin#adm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/sync#shutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:#operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologinnobody:x:99:99:Nobody:/:/sbin/nologinavahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologindbus:x:81:81:System message bus:/:/sbin/nologinpolkitd:x:999:998:User for polkitd:/:/sbin/nologinabrt:x:173:173::/etc/abrt:/sbin/nologinsssd:x:998:997:User for sssd:/:/sbin/nologincolord:x:997:995:User for colord:/var/lib/colord:/sbin/nologinntp:x:38:38::/etc/ntp:/sbin/nologin
sed -n '2p' pp.txt
[root@bogon ~]# sed -n '2p' pp.txt #bin:x:1:1:bin:/bin:/sbin/nologin
sed不具有显示行号功能 配合grep
grep -n '.*' pp.txt |sed '2p' -n[root@bogon ~]# grep -n '.*' pp.txt |sed '2p' -n2:#bin:x:1:1:bin:/bin:/sbin/nologin[root@bogon ~]# grep -n '.*' pp.txt |sed '2,5p' -n2:#bin:x:1:1:bin:/bin:/sbin/nologin3:daemon:x:2:2:daemon:/sbin:/sbin/nologin4:#adm:x:3:4:adm:/var/adm:/sbin/nologin5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
grep -n '.*' pp.txt|sed -n '5,$p'
[root@bogon ~]# grep -n '.*' pp.txt|sed -n '5,$p'5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin6:sync:x:5:0:sync:/sbin:/bin/sync7:#shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown8:halt:x:7:0:halt:/sbin:/sbin/halt9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin10:operator:x:11:0:#operator:/root:/sbin/nologin11:games:x:12:100:games:/usr/games:/sbin/nologin12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin13:nobody:x:99:99:Nobody:/:/sbin/nologin14:avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin15:dbus:x:81:81:System message bus:/:/sbin/nologin16:polkitd:x:999:998:User for polkitd:/:/sbin/nologin17:abrt:x:173:173::/etc/abrt:/sbin/nologin18:sssd:x:998:997:User for sssd:/:/sbin/nologin19:colord:x:997:995:User for colord:/var/lib/colord:/sbin/nologin20:ntp:x:38:38::/etc/ntp:/sbin/nologin
grep -n '.*' pp.txt |sed '/root/p' -n
[root@bogon ~]# grep -n '.*' pp.txt |sed '/root/p' -n1:root:x:0:0:root:/root:/bin/bash10:operator:x:11:0:#operator:/root:/sbin/nologin
[root@bogon ~]# cat 1.txt rtrotrootro t[root@bogon ~]# sed '/ro{1,}t/p' -n -E 1.txt rotroot
查找含有 root或者ftp的行
grep '.*' -n pp.txt |sed -e '/root/p' -e '/ftp/p' -n[root@bogon ~]# grep '.*' -n pp.txt |sed -e '/root/p' -e '/ftp/p' -n1:root:x:0:0:root:/root:/bin/bash10:operator:x:11:0:#operator:/root:/sbin/nologin12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[root@bogon ~]# cat 1.txt phpphp jsjspython[root@bogon ~]# sed -e '/php/p' -e '/js/p' -n 1.txt phpphp jsphp jsjs[root@bogon ~]# sed -e '/php/p' -e '/js/p' 1.txt phpphpphp jsphp jsphp jsjsjspython[root@bogon ~]# sed '/php/p;/js/p' 1.txt -nphpphp jsphp jsjs
cat -n pp.txt |sed '1,5d'
[root@bogon ~]# cat -n pp.txt |sed '1,5d' 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 11 games:x:12:100:games:/usr/games:/sbin/nologin 12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 13 nobody:x:99:99:Nobody:/:/sbin/nologin 14 avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin 15 dbus:x:81:81:System message bus:/:/sbin/nologin 16 polkitd:x:999:998:User for polkitd:/:/sbin/nologin 17 abrt:x:173:173::/etc/abrt:/sbin/nologin 18 sssd:x:998:997:User for sssd:/:/sbin/nologin 19 colord:x:997:995:User for colord:/var/lib/colord:/sbin/nologin 20 ntp:x:38:38::/etc/ntp:/sbin/nologin
不看#开头的注释
cat pp.txt |sed '/^#/d'sed 's/php/python/' 1.txt
sed '1,3s/php/python/' 1.txt[root@bogon ~]# cat 1.txt 11111111123php456123456js123456php[root@bogon ~]# sed 's/php/python/' 1.txt 11111111123python456123456js123456python[root@bogon ~]# sed '1,3s/php/python/' 1.txt 11111111123python456123456js123456php
如果字符串里有/,可使用别的分隔符
/mnt/hgfs/ 替换为 /usr/local/nginx/html
sed 's#/mnt/hgfs/#/usr/local/nginx/html/#' www.conf>www.conf
大小写转换
小写转大写 sed 's/[a-z]/\u&/g' 1.txt sed 's/[A-Z]/\l&/g' 1.txt[root@bogon ~]# cat 1.txt 11111111123php456123456js123456pHp[root@bogon ~]# sed 's/[a-z]/\u&/' 1.txt 11111111123Php456123456Js123456PHp[root@bogon ~]# sed 's/[a-z]/\u&/g' 1.txt 11111111123PHP456123456JS123456PHP[root@bogon ~]# sed 's/[A-Z]/\l&/g' 1.txt 11111111123php456123456js123456php
字符串位置替换
[root@bogon ~]# cat 1.txt a:b:c:da1:b1:c1:d1a2:b2:c2:d2a_: :c:dddd[root@bogon ~]# sed -r 's/([^:]+)(:.*:)([^:]+$)/\3\2\1/' 1.txtd:b:c:ad1:b1:c1:a1d2:b2:c2:a2dddd: :c:a_