2 Replies - 214 Views - Last Post: 11 October 2013 - 01:08 PM Rate Topic: -----

#1 starriol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-October 13

Consolidate several lines of a CSV file with firewall rules

Posted 11 October 2013 - 08:30 AM

Hi guys.
I have a CSV file, which I created using an HTML export from a Check Point firewall policy.
Each rule is represented as several lines, in some cases. That occurs when a rule has several address sources, destinations or services.
I need the output to have each rule described in only one line.
It's easy to distinguish when each rule begins. In the first column, there's the rule ID, which is a number.

Let me show you an example. The strings that should be moved are in bold:

NO.;NAME;SOURCE;DESTINATION;VPN  ;SERVICE;ACTION;TRACK;INSTALL ON;TIME;COMMENT
1;;fwxcluster;mcast_vrrp;;vrrp;accept;Log;fwxcluster;Any;"VRRP;;*Comment suppressed*
;;;;;[b]igmp**;;;;;
2;;fwxcluster;fwxcluster;;FireWall;accept;Log;fwxcluster;Any;"Management FWg;*Comment suppressed*
;;[b]fwmgmpe**;[b]fwmgmpe**;;[b]ssh**;;;;;
;;[b]fwmgm**;[b]fwmgm**;;;;;;;
3;NTP;G_NTP_Clients;cmm_ntpserver_pe01;;ntp;accept;None;fwxcluster;Any;*Comment suppressed*
;;;[b]cmm_ntpserver_pe02**;;;;;;;


What I need ,explained in pseudo code, is this:

Read the first column of the next line. If there's a number:
Evaluate the first column of the next line. If there's no number there, concatenate (separating with a comma) \
the strings in the columns of this line with the last one and eliminate the text in the current one

The output should be something like this. The strings in bold are the ones that were moved:

NO.;NAME;SOURCE;DESTINATION;VPN  ;SERVICE;ACTION;TRACK;INSTALL ON;TIME;COMMENT
1;;fwxcluster,[b]fwmgmpe**,[b]fwmgm**;mcast_vrrp,[b]fwmgmpe**,[b]fwmgm**;;vrrp,[b]ssh**;accept;Log;fwxcluster;Any;*Comment suppressed*
;;;;;;;;;;
;;;;;;;;;;
3;NTP;G_NTP_Clients;cmm_ntpserver_pe01,[b]cmm_ntpserver_pe02**;;ntp;accept;None;fwxcluster;Any;*Comment suppressed*
;;;;;;;;;;


The empty lines are there only to be more clear, I don't actually need them.

So any help getting started would be MOST welcomed, I'm still a newbie and would like to start learning with something I actually need.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Consolidate several lines of a CSV file with firewall rules

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4188
  • View blog
  • Posts: 11,852
  • Joined: 18-April 07

Re: Consolidate several lines of a CSV file with firewall rules

Posted 11 October 2013 - 11:40 AM

Are you sure your examples are accurate because I am looking at them and I am having trouble finding where you put "igmp**" from line 3. If I am understanding you correctly, that should be appearing in the line with marked as "1" in line two. I am also not clear how fwmgmpe and fwmgm got from line 5 of the example into line 2. Shouldn't those appear in line 4 (the one marked with number "2")?

I think you are trying to condense the lines right so you have just lines where the first column is a number and comma separated "destination" fields right? Can you clarify further?
Was This Post Helpful? 0
  • +
  • -

#3 starriol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-October 13

Re: Consolidate several lines of a CSV file with firewall rules

Posted 11 October 2013 - 01:08 PM

Hi Martyr2, thanks for pointing out the error.
I made a mess and now I can't edit my original post :blink:/>

I'll correct it here and post an image to explain it more clearly.

Posted Image

Original CSV:

NO.;NAME;SOURCE;DESTINATION;SERVICE;ACTION;
1;;fwgcluster;mcast_vrrp;vrrp;accept;
;;;;igmp;;
2;Testing;fwgcluster;fwgcluster;FireWall;accept;
;;fwmgmpe;fwmgmpe;ssh;;
;;fwmgm;fwmgm;;;

After running the script:

NO.;NAME;SOURCE;DESTINATION;SERVICE;ACTION;
1;;fwgcluster;mcast_vrrp;vrrp-igmp;accept;
;;;;;;
2;Testing;fwgcluster-fwmgmpe-fwmgm;fwgcluster-fwmgmpe-fwmgm;FireWall-ssh;accept;
;;;;;;
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1