简体   繁体   English

使用sed替代单个记录

[英]Using sed to substitute in a single record

I am automating a security policy creation and I have the records below, including source/dst IP, src/dst/zone, policy name and port. 我正在自动执行安全策略创建,并且具有以下记录,包括源/ dst IP,src / dst / zone,策略名称和端口。 I can easily move the record around and insert text between them using awk, but I stuck at a point when I need to modify an individual record and keep the rest the same. 我可以轻松地移动记录,并使用awk在它们之间插入文本,但是当我需要修改单个记录并使其余记录保持不变时,我遇到了麻烦。 As in the below, in the 3rd record I need to replace "." 如下所示,在第三条记录中,我需要替换“。” with "_" and "/" with "-", for the reason that "." 用“ _”表示,“ /”用“-”表示,原因是“。”。 and "/" are notsupported in policy names. 和“ /”在策略名称中不受支持。

net-10.1.24.0/23  net-10.20.0.0/27  net-10.1.24.0/23_net-10.20.0.0/27   vpn-zone    internal-zone-servers   udp-123
net-10.1.24.0/23  net-10.20.0.0/27  net-10.1.24.0/23_net-10.20.0.0/27   vpn-zone    internal-zone-users grp-app-ldap
net-10.3.223.8/29 net-10.20.0.0/27  net-10.3.223.8/29_net-10.20.0.0/27  dmz-zone1   internal-zone-users tcp-389
net-10.1.194.64/26 net-10.20.0.64/27    net-10.1.194.64/26_net-10.20.0.64/27    dmz-zone2   internal-zone-servers   tcp-8080

I need to get to something like: 我需要做类似的事情:

net-10.1.24.0/23    net-10.20.0.0/27  net-10_1_24_0-23_net-10_20_0_0-27 vpn-zone    internal-zone-servers   udp-123

So far I achieved the same result but saving 3rd record to a new file, substiuting "." 到目前为止,我获得了相同的结果,但是将第3条记录保存到一个新文件中,并替换为“”。 and "/" with sed and then using "paste file1 file2" to join them together. 和“ /”与sed,然后使用“ paste file1 file2”将它们连接在一起。 Also it can be done by a script. 也可以通过脚本来完成。 but I was hoping that you can advise on more intelligent and simple one line awk/sed solution. 但我希望您可以就更智能,更简单的单行awk / sed解决方案提供建议。

I would do 2 substitutions on the third field then print the line: 我会在第三个字段上进行2次替换,然后打印该行:

 awk '{gsub("[.]","_",$3);gsub("[/]","-",$3); print $0}' policy.txt
net-10.1.24.0/23 net-10.20.0.0/27 net-10_1_24_0-23_net-10_20_0_0-27 vpn-zone internal-zone-servers udp-123
net-10.1.24.0/23 net-10.20.0.0/27 net-10_1_24_0-23_net-10_20_0_0-27 vpn-zone internal-zone-users grp-app-ldap
net-10.3.223.8/29 net-10.20.0.0/27 net-10_3_223_8-29_net-10_20_0_0-27 dmz-zone1 internal-zone-users tcp-389
net-10.1.194.64/26 net-10.20.0.64/27 net-10_1_194_64-26_net-10_20_0_64-27 dmz-zone2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM