perl命令:批量修改替換檔案

weixin_34205076發表於2017-11-26

需求如下:有nginx負載均衡配置檔案,更新某幾臺伺服器需要先註釋掉前端機器,需要用指令碼實現;

之前一直用sed進行替換,遇到一個高手用perl命令也可以實現,命令更簡潔直觀,可以實現同樣的效果。


舉例如下:

[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 -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





本文轉自 模範生 51CTO部落格,原文連結:http://blog.51cto.com/mofansheng/1920369,如需轉載請自行聯絡原作者

相關文章