onur
onur

Reputation: 6365

How can I parse and convert ip database?

I have ip database (ripe-apnic) like this:

inetnum:      218.75.100.64 - 218.75.100.67
netname:      YONGKANG-SHIJI-NETBAR
country:      CN
descr:        Yongkang Shiji Internet Bar
descr:        NULL
admin-c:      XY203-AP
tech-c:       CJ54-AP
status:       ASSIGNED NON-PORTABLE
changed:      [email protected] 20040610
mnt-by:       MAINT-CN-CHINANET-ZJ-JH
source:       APNIC

inetnum:        218.75.99.0 - 218.75.99.3
netname:        WEISHENG-COLTD
country:        CN
descr:          Donggan Hospital
descr:
admin-c:        DS1202-AP
tech-c:         CJ54-AP
mnt-irt:        IRT-CHINANET-ZJ
status:         ASSIGNED NON-PORTABLE
changed:        [email protected] 20150510
mnt-by:         MAINT-CN-CHINANET-ZJ-JH
source:         APNIC
...

I need to convert to csv file (just ipstart-ipend-netname-country-descr1-source) like this:

218.75.100.64;218.75.100.67;YONGKANG-SHIJI-NETBAR;CN;Yongkang Shiji Internet Bar;APNIC
218.75.99.0;218.75.99.3;WEISHENG-COLTD;CN;Donggan Hospital;APNIC
...

How can I do this with awk or bash?

Upvotes: 1

Views: 297

Answers (1)

anubhava
anubhava

Reputation: 785098

Using awk you can do:

awk -F ':[[:blank:]]*' '/inetnum:/{ip=$2; sub(/ +- +/, ";", ip)} /netname:/{nn=$2}
   /country:/{ct=$2} ds=="" && /descr:/{ds=$2}
  /source:/{print ip, nn, ct, ds, $2; ds=""}' OFS=";" file

Output:

218.75.100.64;218.75.100.67;YONGKANG-SHIJI-NETBAR;CN;Yongkang Shiji Internet Bar;APNIC
218.75.99.0;218.75.99.3;WEISHENG-COLTD;CN;Donggan Hospital;APNIC

This will ignore second descr field.

Upvotes: 2

Related Questions