需求以下:有nginx負載均衡配置文件,更新某幾臺服務器須要先註釋掉前端機器,須要用腳本實現;前端
以前一直用sed進行替換,遇到一個高手用perl命令也能夠實現,命令更簡潔直觀,能夠實現一樣的效果。nginx
舉例以下:服務器
[root@localhost ~]# cat host.conf負載均衡
upstream test_server {ide
server 192.168.169.36:80;spa
server 192.168.169.37:80;server
server 192.168.169.38:80;ip
server 192.168.169.39:80;it
server 192.168.169.50:80;ast
server 192.168.169.51:80;
server 192.168.169.52:80;
}
打印匹配的行
[root@localhost ~]# perl -ne 'print if /192.168.169.3/' host.conf
server 192.168.169.36:80;
server 192.168.169.37:80;
server 192.168.169.38:80;
server 192.168.169.39:80;
[root@localhost ~]# perl -ne 'print if /192.168.169.5/' host.conf
server 192.168.169.50:80;
server 192.168.169.51:80;
server 192.168.169.52:80;
perl命令:匹配192.168.169.3 網段的前面加#;
-i 寫入到文件
[root@localhost ~]# perl -i -pe 's/(.*)/#$1/ if /192.168.169.3/' host.conf
執行結果以下:
[root@localhost ~]# cat host.conf
upstream test_server {
#server 192.168.169.36:80;
#server 192.168.169.37:80;
#server 192.168.169.38:80;
#server 192.168.169.39:80;
server 192.168.169.50:80;
server 192.168.169.51:80;
server 192.168.169.52:80;
}
同理,去掉#
[root@localhost ~]# perl -i -pe 's/#(.*)/$1/ if /192.168.169.3/' host.conf
[root@localhost ~]# cat host.conf
upstream test_server {
server 192.168.169.36:80;
server 192.168.169.37:80;
server 192.168.169.38:80;
server 192.168.169.39:80;
server 192.168.169.50:80;
server 192.168.169.51:80;
server 192.168.169.52:80;
}
匹配192.168.169. 網段的全部機器加#
[root@localhost ~]# perl -i -pe 's/(.*)/#$1/ if /192.168.169./' host.conf
[root@localhost ~]# cat host.conf
upstream test_server {
#server 192.168.169.36:80;
#server 192.168.169.37:80;
#server 192.168.169.38:80;
#server 192.168.169.39:80;
#server 192.168.169.50:80;
#server 192.168.169.51:80;
#server 192.168.169.52:80;
}
perl命令也支持相似sed的關鍵字替換
[root@localhost ~]# perl -pe 's/server/master/ if /192.168.169.3/' host.conf
upstream test_server {
master 192.168.169.36:80;
master 192.168.169.37:80;
master 192.168.169.38:80;
master 192.168.169.39:80;
server 192.168.169.50:80;
server 192.168.169.51:80;
server 192.168.169.52:80;
}
問題來了,實際配置文件基本都是同一個網段的ip,若是匹配的話,會匹配到其餘機器上面,問題就嚴重了,因此須要按指定行進行精確匹配;
[root@localhost ~]# cat host.conf
upstream test_server {
server 192.168.169.36:80;
server 192.168.169.37:80;
server 192.168.169.38:80;
server 192.168.169.39:80;
server 192.168.169.50:80;
server 192.168.169.51:80;
server 192.168.169.52:80;
}
perl命令,匹配指定的行
if 判斷 $. 行數,與或關係進行匹配;
[root@localhost ~]# perl -ne 'print if $.==2 ' host.conf
server 192.168.169.36:80;
[root@localhost ~]# perl -ne 'print if $.==4 ' host.conf
server 192.168.169.38:80;
[root@localhost ~]# perl -ne 'print if $.>=2 && $.<=5 ' host.conf
server 192.168.169.36:80;
server 192.168.169.37:80;
server 192.168.169.38:80;
server 192.168.169.39:80;
精確匹配,進行替換,大於等於第2行,小於等於第5行;
[root@localhost ~]# perl -pe 's/(.*)/#$1/ if $.>=2 && $.<=5' host.conf
upstream nav1_server {
#server 192.168.169.36:80;
#server 192.168.169.37:80;
#server 192.168.169.38:80;
#server 192.168.169.39:80;
server 192.168.169.50:80;
server 192.168.169.51:80;
server 192.168.169.52:80;
}
加-i 參數,將修改的操做寫入到文件中;
[root@localhost ~]# perl -i -pe 's/(.*)/#$1/ if $.>=2 && $.<=5' host.conf