我在文件中有以下行
<getTheIP ConnectType='INFO' Host='machine_num_1' VirtualIp='12.34.3.9'/>
我需要将IP 12.34.3.9更改为其他IP,例如3.4.5.6 但只有当我排在第一个单词 ConnectType 并且第二个单词匹配 machine_num_1
时请使用perl one line commnand建议我如何做到这一点(我需要从bash脚本运行这个perl line命令)
答案 0 :(得分:2)
>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'/>