Using sed to substitute in a single record

52 views Asked by At

I am automating a security policy creation and I have the records below, including source/dst IP, src/dst/zone, policy name and port. 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. 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 "." and "/" with sed and then using "paste file1 file2" to join them together. 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.

1

There are 1 answers

0
Tensibai On

I would do 2 substitutions on the third field then print the line:

 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