Home >Operation and Maintenance >Linux Operation and Maintenance >sed usage details
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
Note: Only when in is matched for the second time in each line will it be replaced with 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
Note: You can use the -n -p combination
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
Note: You can use $ to indicate the end
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 multi-command execution:
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
The above is the detailed content of sed usage details. For more information, please follow other related articles on the PHP Chinese website!