sed -e 's#/#-#g' -e's#:#-#g' passwd
vim 123.sed s#:#/#g s#/#-#g
sed -f 123.sed passwd
sed -n 2p passwd
sed 's/in/AAAAA/2' passwd
root:x:0:0:root:/root:/bin/bashbin:x:1:1:bAAAAA:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbAAAAA/nologin
注意:在每一行内第二次匹配到in,才会替换成AAAAA
sed '2s/in/AAAAA/2' passwd
root:x:0:0:root:/root:/bin/bashbin:x:1:1:bAAAAA:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin
注意:可以使用-n -p 组合
sed -n '2s/in/AAAAA/2p' passwd bin:x:1:1:bAAAAA:/bin:/sbin/nologin
sed -n '2,3s/in/AAAAA/2p' passwd
bin:x:1:1:bAAAAA:/bAAAAA:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbAAAAA/nologin
sed -n '2,$s/in/AAAAA/2p' passwd
注意:可以使用$表示到结尾
sed -n '2s/in/AAAAA/2pw passwd.sed' passwd bin:x:1:1:bAAAAA:/bin:/sbin/nologin
cat passwd.sed bin:x:1:1:bAAAAA:/bin:/sbin/nologin
sed -i.bak '2s/in/AAAAA/2' passwd
cat -n passwd1 root:x:0:0:root:/root:/bin/bash2 bin:x:1:1:bAAAAA:/bin:/sbin/nologin
ls passwd*passwd passwd.bak
sed -n '/root/s/bin/AAAAA/p' passwd
root:x:0:0:root:/root:/AAAAA/bashoperator:x:11:0:operator:/root:/sAAAAA/nologin
9,sed多命令执行:
sed '1{s/root/ROOT/s/bin/BIN/ }' passwd
ROOT:x:0:0:root:/root:/BIN/bash
sed -n -e '1s/root/ROOT/' -e '1s/bin/BIN/' -e 1p passwd
ROOT:x:0:0:root:/root:/BIN/bash
以上是sed 用法详述的详细内容。更多信息请关注PHP中文网其他相关文章!