知乎專欄 | 多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者” |
http://sed.sourceforge.net/
sed -n 's/root/admin/p' /etc/passwd sed -n 's/root/admin/2p' /etc/passwd #在每行的第2個root作替換 sed -n 's/root/admin/gp' /etc/passwd sed -n '1,10 s/root/admin/gp' /etc/passwd sed -n 's/root/AAA&BBB/2p' /etc/passwd #將root替換成AAArootBBB,&作反向引用,代替前面的匹配項 sed -ne 's/root/AAA&BBB/' -ne 's/bash/AAA&BBB/p' /etc/passwd #-e將多個命令連接起來,將root或bash行作替換 sed -n 's/root/AAA&BBB/;s/bash/AAA&BBB/p' /etc/passwd #與上命令功能相同 sed -nr 's/(root)(.*)(bash)/\3\2\1/p' /etc/passwd #將root與bash位置替換,兩標記替換 或sed -n 's/root.∗bash/\3\2\1/p' /etc/passwd
ls -1 *.html| awk '{printf "sed \047s/ADDRESS/address/g\047 %s >%s.sed;mv %s.sed %s\n", $1, $1, $1, $1;}'|bash for f in `ls -1 *.html`; do [ -f $f ] && sed 's/<\/BODY>/<script src="http:\/\/www.google-analytics.com\/urchin.js" type="text\/javascript"><\/script>\n<script type="text\/javascript">\n_uacct = "UA-2033740-1";\nurchinTracker();\n<\/script>\n<\/BODY>/g' $f >$f.sed;mv $f.sed $f ; done;
my=/root/dir str="/root/dir/file1 /root/dir/file2 /root/dir/file3 /root/dir/file/file1" echo $str | sed "s:$my::g"
$ echo "aaa=\"bbb\"" | sed 's/.*=\"\(.*\)\"/\1/g' $ curl -s http://www.example.com | egrep -o '<a href="(.*)">.*</a>' | sed -e 's/.*href="\([^"]*\)".*/\1/'
Mac 地址轉換
echo 192.168.2.1-a1f4.40c1.5756 | sed -r 's|(.*-)(..)(..).(..)(..).(..)(..)|\1\2:\3:\4:\5:\6:\7|g'
i 命令插入一行,並且在當前行前面有兩個空格
在root行前插入一個admin
sed '/root/i admin' /etc/passwd
33 行處插入字元
sed -i "33 i \ \ authorization: enabled" /etc/mongod.conf
刪除含有root的行
sed '/root/d' /etc/passwd
模式空間中的內容全部打印出來
定位行:
sed -n '12,~3p' pass #從第12行開始,直到下一個3的倍數行(12-15行) sed -n '12,+4p' pass #從第12行開始,連續4行(12-16行) sed -n '12~3p' pass #從第12行開始,間隔3行輸出一次(12,15,18,21...) sed -n '10,$p' pass #從第10行至結尾 sed -n '4!p' pass #除去第4行
打印3~6行間的內容
$ sed -n '3,6p' /etc/passwd bin:x:2:2:bin:/bin:/usr/sbin/nologin sys:x:3:3:sys:/dev:/usr/sbin/nologin sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/usr/sbin/nologin
打印35行至行尾
$ sed -n '35,$p' /etc/passwd sshd:x:116:65534::/var/run/sshd:/usr/sbin/nologin mysql:x:117:126:MySQL Server,,,:/nonexistent:/bin/false uuidd:x:100:101::/run/uuidd:/bin/false libvirt-qemu:x:118:128:Libvirt Qemu,,,:/var/lib/libvirt:/bin/false libvirt-dnsmasq:x:119:129:Libvirt Dnsmasq,,,:/var/lib/libvirt/dnsmasq:/bin/false redis:x:120:130::/var/lib/redis:/bin/false
-i[SUFFIX], --in-place[=SUFFIX] edit files in place (makes backup if extension supplied)
下面例子是替換t.php中的java字元串為php
$ cat t.php <?java $ sed -i 's/java/php/g' t.php $ cat t.php <?php
find -name "*.php" -exec sed -i '/<?.*eval(gzinflate(base64.*?>/ d' '{}' \; -print
指定查找替換的行號
sed -i "7,7 s/#server.host: \"localhost\"/server.host: \"0.0.0.0\"/" /etc/kibana/kibana.yml
正則:'/正則式/'
sed -n '/root/p' /etc/passwd sed -n '/^root/p' /etc/passwd sed -n '/bash$/p' /etc/passwd sed -n '/ro.t/p' /etc/passwd sed -n '/ro*/p' /etc/passwd sed -n '/[ABC]/p' /etc/passwd sed -n '/[A-Z]/p' /etc/passwd sed -n '/[^ABC]/p' /etc/passwd sed -n '/^[^ABC]/p' /etc/passwd sed -n '/\<root/p' /etc/passwd sed -n '/root\>/p' /etc/passwd
擴展正則:
sed -n '/root\|yerik/p' /etc/passwd #拓展正則需要轉義 sed -nr '/root|yerik/p' /etc/passwd #加-r參數支持拓展正則 sed -nr '/ro(ot|ye)rik/p' /etc/passwd #匹配rootrik和royerik單詞 sed -nr '/ro?t/p' /etc/passwd #?匹配0-1次前導字元 sed -nr '/ro+t/p' /etc/passwd #匹配1-n次前導字元 sed -nr '/ro{2}t/p' /etc/passwd #匹配2次前導字元 sed -nr '/ro{2,}t/p' /etc/passwd #匹配多於2次前導字元 sed -nr '/ro{2,4}t/p' /etc/passwd #匹配2-4次前導字元 sed -nr '/(root)*/p' /etc/passwd #匹配0-n次前導單詞
cat <<! | sed '/aaa=\(bbb\|ccc\|ddd\)/!s/\(aaa=\).*/\1xxx/' > aaa=bbb > aaa=ccc > aaa=ddd > aaa=[something else] ! aaa=bbb aaa=ccc aaa=ddd aaa=xxx
[root@localhost ~]# echo "netkiller" | sed 's/[a-z]/\u&/g' NETKILLER [root@localhost ~]# echo "NETKILLER" | sed 's/[A-Z]/\l&/g' netkiller