shell sed i 指定內容追加

2022-03-24 00:08:59 字數 1248 閱讀 9643

[root@testvm02 ~]# cat nrpe.cfg 

#command[check_users]=/usr/local/nagios/libexec/check_users -w $arg1$ -c $arg2$

#command[check_load]=/usr/local/nagios/libexec/check_load -w $arg1$ -c $arg2$

#command[check_disk]=/usr/local/nagios/libexec/check_disk -w $arg1$ -c $arg2$ -p $arg3$

#command[check_procs]=/usr/local/nagios/libexec/check_procs -w $arg1$ -c $arg2$ -s $arg3$

2.通過sed實現在搜尋到某個字串之後,在下一行插入字串

[root@testvm02 ~]# sed -i '/#command\[check_procs\]

/a\command\[check_disk\]=\/usr\/local\/nagios\/libexec\/check_disk -w 15\% -c 10\% -p \/ -c -w 15\% -c 10\% -p \/mnt' nrpe.cfg

[root@testvm02 ~]# cat nrpe.cfg

#command[check_users]=/usr/local/nagios/libexec/check_users -w $arg1$ -c $arg2$

#command[check_load]=/usr/local/nagios/libexec/check_load -w $arg1$ -c $arg2$

#command[check_disk]=/usr/local/nagios/libexec/check_disk -w $arg1$ -c $arg2$ -p $arg3$

#command[check_procs]=/usr/local/nagios/libexec/check_procs -w $arg1$ -c $arg2$ -s $arg3$

command[check_disk]=/usr/local/nagios/libexec/check_disk -w 15% -c 10% -p / -c -w 15% -c 10% -p /mnt

#黃色是原字串,藍色是要插入的字串,紅色表示的是在匹配的字串的下一行插入

另,a後面的\可以不需要:

Python追加檔案內容

測試中需要造幾百個賬號,寫了個指令碼可以自動生成賬號,但想把生成的賬號寫入乙個檔案,開始用的如下的write 方法,發下會先把原檔案的內容清空再寫入新的東西,檔案裡面每次都是最新生成的乙個賬號 mobile method.createphone file r d test.txt with open...

Python追加檔案內容

測試中需要造幾百個賬號,寫了個指令碼可以自動生成賬號,但想把生成的賬號寫入乙個檔案,開始用的如下的write 方法,發下會先把原檔案的內容清空再寫入新的東西,檔案裡面每次都是最新生成的乙個賬號 mobile method.createphone file r d test.txt with open...

Python追加檔案內容

個人分類 python 測試中需要造幾百個賬號,寫了個指令碼可以自動生成賬號,但想把生成的賬號寫入乙個檔案,開始用的如下的write 方法,發下會先把原檔案的內容清空再寫入新的東西,檔案裡面每次都是最新生成的乙個賬號 mobile method.createphone file r d test.t...