开发者

perl + one line change string only if match two words in line

开发者 https://www.devze.com 2023-01-29 20:34 出处:网络
I have the following line in file <getTheIP ConnectType=\'INFO\' Host=\'machine_num_1\' VirtualIp=\'12.34.3.9\'/>

I have the following line in file

    <getTheIP ConnectType='INFO' Host='machine_num_1' VirtualIp='12.34.3.9'/>

I need to change the IP 12.34.3.9 to other IP for example 3.4.5.6 but only if I have in line the first word match ConnectType and the second word match machine_num_1

Please advice how I can do开发者_运维技巧 that with perl one line commnand ( I need to run this perl line command from bash script)


>cat test.txt
 <getTheIP ConnectType='INFO' Host='machine_num_1' VirtualIp='12.34.3.9'/>
 <getTheIP ConnectFour='INFO' Host='machine_num_41' VirtualIp='12.34.3.9'/>
 <getTheIP ConnectThree='INFO' Host='machine_num_31' VirtualIp='12.34.3.9'/>
 <getTheIP ConnectType='INFO' Host='machine_num_21' VirtualIp='12.34.3.9'/>

>perl -lpe "if (/ConnectType/ && /'machine_num_1'/) {s/(\d{1,3}\.){3}\d{1,3}/3.4.5.6/};" test.txt
 <getTheIP ConnectType='INFO' Host='machine_num_1' VirtualIp='3.4.5.6'/>
 <getTheIP ConnectFour='INFO' Host='machine_num_41' VirtualIp='12.34.3.9'/>
 <getTheIP ConnectThree='INFO' Host='machine_num_31' VirtualIp='12.34.3.9'/>
 <getTheIP ConnectType='INFO' Host='machine_num_21' VirtualIp='12.34.3.9'/>
0

精彩评论

暂无评论...
验证码 换一张
取 消