sed/awk搜索一条线上的字符串中的模式,并在另一行上的字符串中更改模式



在f5 bigip.conf文本文件中,有数百个虚拟服务器条目如下。我需要将路由域从701更改为703,但仅当虚拟服务器目标地址包含10.166.201时。 - 10.166.201.48%701

sed '/10.166.201./s/701/703/' bigip.conf

我现在的问题是,后续源NAT条目"源0.0.0.0.0%701/0" 也必须更改,但仅适用于第一个搜索标准范围内的实例。服务器目标地址包含10.166.201

应匹配并更改:

ltm virtual /Common/vip_181_vs {
destination /Common/10.166.201.48%701:81
ip-protocol tcp
mask 255.255.255.255
persist {
    /Common/sbe017_81_cookie {
        default yes
    }
}
pool /Common/sbe017_81_pool
profiles {
    /Common/oneconnect-32 { }
    /Common/swt_http { }
    /Common/tcp_3600 { }
}
rules {
    /Common/rule_dev
}
source 0.0.0.0%701/0
source-address-translation {
    pool /Common/natpool-701_swt_cfr_v0081
    type snat
}
translate-address enabled
translate-port enabled
}

应该不匹配或更改:

ltm virtual /Common/vip_182_vs {
destination /Common/10.155.201.44%701:81
ip-protocol tcp
mask 255.255.255.255
persist {
    /Common/sbe017_81_cookie {
        default yes
    }
}
pool /Common/sbe017_81_pool
profiles {
    /Common/oneconnect-32 { }
    /Common/swt_http { }
    /Common/tcp_3600 { }
}
rules {
    /Common/rule_dev
}
source 0.0.0.0%701/0
source-address-translation {
    pool /Common/natpool-701_swt_cfr_v0081
    type snat
}
translate-address enabled
translate-port enabled
}

如果您的Input_file有多个DestinationSource字符串,则您需要在每次Destination字符串是否具有特定值的情况下检查条件,然后以下内容可能会执行技巧。

awk '/your_ip/{sub(/701/,"703");flag=1} flag && /source 0.0.0.0/{sub(/701/,"703");flag=""} 1'  Input_file

如果要将输出保存在input_file本身中,则执行以下操作:

awk '/your_ip/{sub(/701/,"703");flag=1} flag && /source 0.0.0.0/{sub(/701/,"703");flag=""} 1'  Input_file  > temp_file  && mv temp_file  Input_file

your_ip是您的地址并逃脱eg-> . as .也在其中。

最新更新