首页 > 代码库 > sed命令常见用法
sed命令常见用法
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‘ -n
sed ‘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
sed ‘s/[A-Z]/\l&/g‘ 1.txt
-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/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 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:#operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:998:User for polkitd:/:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin sssd:x:998:997:User for sssd:/:/sbin/nologin colord:x:997:995:User for colord:/var/lib/colord:/sbin/nologin ntp: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‘ -n 2:#bin:x:1:1:bin:/bin:/sbin/nologin [root@bogon ~]# grep -n ‘.*‘ pp.txt |sed ‘2,5p‘ -n 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
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/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
grep -n ‘.*‘ pp.txt |sed ‘/root/p‘ -n
[root@bogon ~]# grep -n ‘.*‘ pp.txt |sed ‘/root/p‘ -n 1:root:x:0:0:root:/root:/bin/bash 10:operator:x:11:0:#operator:/root:/sbin/nologin
[root@bogon ~]# cat 1.txt rt rot root ro t [root@bogon ~]# sed ‘/ro{1,}t/p‘ -n -E 1.txt rot root
查找含有 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‘ -n 1:root:x:0:0:root:/root:/bin/bash 10:operator:x:11:0:#operator:/root:/sbin/nologin 12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[root@bogon ~]# cat 1.txt php php js js python [root@bogon ~]# sed -e ‘/php/p‘ -e ‘/js/p‘ -n 1.txt php php js php js js [root@bogon ~]# sed -e ‘/php/p‘ -e ‘/js/p‘ 1.txt php php php js php js php js js js python [root@bogon ~]# sed ‘/php/p;/js/p‘ 1.txt -n php php js php js js
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 11111111 123php456 123456js 123456php [root@bogon ~]# sed ‘s/php/python/‘ 1.txt 11111111 123python456 123456js 123456python [root@bogon ~]# sed ‘1,3s/php/python/‘ 1.txt 11111111 123python456 123456js 123456php
大小写转换
小写转大写
sed ‘s/[a-z]/\u&/g‘ 1.txt
sed ‘s/[A-Z]/\l&/g‘ 1.txt
[root@bogon ~]# cat 1.txt 11111111 123php456 123456js 123456pHp [root@bogon ~]# sed ‘s/[a-z]/\u&/‘ 1.txt 11111111 123Php456 123456Js 123456PHp [root@bogon ~]# sed ‘s/[a-z]/\u&/g‘ 1.txt 11111111 123PHP456 123456JS 123456PHP [root@bogon ~]# sed ‘s/[A-Z]/\l&/g‘ 1.txt 11111111 123php456 123456js 123456php
字符串位置替换
[root@bogon ~]# cat 1.txt a:b:c:d a1:b1:c1:d1 a2:b2:c2:d2 a_: :c:dddd [root@bogon ~]# sed -r ‘s/([^:]+)(:.*:)([^:]+$)/\3\2\1/‘ 1.txt d:b:c:a d1:b1:c1:a1 d2:b2:c2:a2 dddd: :c:a_
sed命令常见用法