sed 用法,sed

來源:互聯網
上載者:User

sed 用法,sed
sed 用法1,替換符號:

sed -e 's#/#-#g' -e's#:#-#g' passwd
2,使用sed檔案輸入命令:
vim 123.seds#:#/#gs#/#-#g
sed -f 123.sed passwd
3,列印:
sed -n 2p passwd
4,替換第n次匹配:
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

5,替換第n行的第m次匹配:
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

注意:可以使用$表示到結尾

6,將替換結果儲存到檔案中:
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
7,將替換結果備份,並且修改源檔案:
sed -i.bak '2s/in/AAAAA/2' passwd
cat -n  passwd1root:x:0:0:root:/root:/bin/bash2bin:x:1:1:bAAAAA:/bin:/sbin/nologin
ls passwd*passwd  passwd.bak
8,條件過濾修改:
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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.