0

I have configuration file in xml format which I want to convert into set commands for firewall configuration.

        <entry name="server1">
            <ip-netmask>1.1.1.1</ip-netmask>
            <description>server1</description>
        </entry>
        <entry name="server2">
            <ip-netmask>2.2.2.2</ip-netmask>
            <description>server2</description>
        </entry>

Want to convert to

set address server1 ip-netmask 1.1.1.1
set address server2 ip-netmask 2.2.2.2

even if it is server1 1.1.1.1 I can try to use echo to change them, Tried using sed and awk, failing to get desired output, can someone help?

builder-7000
  • 7,131
  • 3
  • 19
  • 43
user140452
  • 133
  • 7

2 Answers2

0

This will help you. It will work as long as your input is exactly as you have posted. Anyway, as mentioned by daniu, please use a specific tool for this task :

 sed 's/^ *//g' input.txt | grep -E -v '^(<entry name|</entry)' | sed -E 's#</?(i|d)[a-zAZ-]+>##g' | awk  'BEGIN {ORS=""} !/^ *$/ { if (ticks == 2) { print "\n" ; ticks = 0}; print $1" "; ticks++}' | awk '{ print "set address "$2 " ip-mask "$1" " }'

Regards!

Matias Barrios
  • 4,674
  • 3
  • 22
  • 49
0

With sed and paste you could do:

sed -n 's/.*entry name="\([^"]*\)".*/set address \1/p;
        s/.*<ip-netmask>\([^<]*\)<.*/ip-netmask \1/p' inputfile |\
        paste -d ' ' - -
builder-7000
  • 7,131
  • 3
  • 19
  • 43