Print "From: To:" Row Only On First Ocurrence

Hi guys,

I am trying to finish up the script I created that parsed, and sorted my firewall rules, however, I am having a little problem finishing it up.

In my output, above each policy I always have a "From: To:" line, but since I was able to sort it, this is somewhat redundant in the output. Unfortunately I cannot just sort the file and run uniq on it as that would throw the sorting I painstakingly got working completely off, as well as possibly remove parts of the firewall policies I am trying to get printed out.

Some of the input I am working with looks like this:

From:  Wlan       To:    Untrust-Firn
567   | Any                       | State Auditor VPN         | grp-vpn                   | Permit          | alw-state-vpn

From:  Wlan       To:    Untrust-Firn
594   | Any                       | Any                       | GRE                       | Permit          | alw-vpn   
      |                           |                           | grp-vpn                  

From:  DMZ-public To:    VPN-cs    
506   | Polycom-COB-2320          | aaaaaaaaa-Polycom         | H.323                     | Permit          | alw-polycom
      |                           |                           | PING                     
      |                           |                           | Polycom-Call-Setup       

From:  DMZ-public To:    VPN-cs    
517   | Polycom-BB-ConfRm         | aaaaaa-Polycom            | ANY-4hrs                  |                 | alw-polycom

From:  Trust-cs   To:    VPN-cs    
351   | Any                       | Any                       | Any-to-8-hrs              | Permit          |           
      |                           |                           | PING                     

From:  Trust-cs   To:    Wlan      
426   | grp-trust-dhcpservers     | Any                       | DHCP-Relay                | Permit          | alw-dhcp  

From:  Trust-cs   To:    Wlan      
436   | Any                       | Any                       | PING                      | Permit          | alw-ping  
      |                           |                           | SSH                      

From:  Trust-cs   To:    Wlan      
510   | abcd-epicenter.primary.ad | Guest-Router              | SNMP                      | Permit          | alw-snmp  
      | netmon.noc.xxxx.xxx       |                           |                          

I searched the forums and tried to find an answer in the numerous other postings that dealt with removing duplicate lines but I could not find anything that gave me the exact result.

I tried radoulov's suggestion from another post on my RHEL install:

awk 'x[$0]++' input.txt

but that also removed parts of the firewall policies that I needed to keep.

I am just trying to remove repeated lines such as (keeping the first ocurrance each time):
From: DMZ-public To: VPN-cs

I'd like the final output to look similar to this:

From:  Wlan       To:    Untrust-Firn
427   | Any                       | Any                       | POP3                      | Permit          | alw-pop3  

428   | Any                       | Any                       | SMTP                      | Permit          | alw-smtp  

469   | Alva-T1-FW                | Any                       | ANY                       | Permit          | alw-vp-t1 

From:  Trust-cs  To:    Untrust-Firn
533   | Any                       | grp-untrust-aplia         | Aplia (wo 73857)          | Permit          | alw-aplia-ports

Thank you in advance for any help you can give me. It is much appreciated.

Here's a Perl one-liner that should do the job:

perl -lne 'print if !/^From/ or (/^From/ and $_ ne $p); $p=$_ if /^From/' your_file

Test run -

$ 
$ cat firewall.rules
From:  Wlan       To:    Untrust-Firn
567   | Any                       | State Auditor VPN         | grp-vpn                   | Permit          | alw-state-vpn

From:  Wlan       To:    Untrust-Firn
594   | Any                       | Any                       | GRE                       | Permit          | alw-vpn   
      |                           |                           | grp-vpn                  

From:  DMZ-public To:    VPN-cs    
506   | Polycom-COB-2320          | aaaaaaaaa-Polycom         | H.323                     | Permit          | alw-polycom
      |                           |                           | PING                     
      |                           |                           | Polycom-Call-Setup       

From:  DMZ-public To:    VPN-cs    
517   | Polycom-BB-ConfRm         | aaaaaa-Polycom            | ANY-4hrs                  |                 | alw-polycom

From:  Trust-cs   To:    VPN-cs    
351   | Any                       | Any                       | Any-to-8-hrs              | Permit          |           
      |                           |                           | PING                     

From:  Trust-cs   To:    Wlan      
426   | grp-trust-dhcpservers     | Any                       | DHCP-Relay                | Permit          | alw-dhcp  

From:  Trust-cs   To:    Wlan      
436   | Any                       | Any                       | PING                      | Permit          | alw-ping  
      |                           |                           | SSH                      

From:  Trust-cs   To:    Wlan      
510   | abcd-epicenter.primary.ad | Guest-Router              | SNMP                      | Permit          | alw-snmp  
      | netmon.noc.xxxx.xxx       |                           |
$ 
$ 
$ # Run the Perl one-liner
$ perl -lne 'print if !/^From/ or (/^From/ and $_ ne $p); $p=$_ if /^From/' firewall.rules
From:  Wlan       To:    Untrust-Firn
567   | Any                       | State Auditor VPN         | grp-vpn                   | Permit          | alw-state-vpn

594   | Any                       | Any                       | GRE                       | Permit          | alw-vpn   
      |                           |                           | grp-vpn                  

From:  DMZ-public To:    VPN-cs    
506   | Polycom-COB-2320          | aaaaaaaaa-Polycom         | H.323                     | Permit          | alw-polycom
      |                           |                           | PING                     
      |                           |                           | Polycom-Call-Setup       

517   | Polycom-BB-ConfRm         | aaaaaa-Polycom            | ANY-4hrs                  |                 | alw-polycom

From:  Trust-cs   To:    VPN-cs    
351   | Any                       | Any                       | Any-to-8-hrs              | Permit          |           
      |                           |                           | PING                     

From:  Trust-cs   To:    Wlan      
426   | grp-trust-dhcpservers     | Any                       | DHCP-Relay                | Permit          | alw-dhcp  

436   | Any                       | Any                       | PING                      | Permit          | alw-ping  
      |                           |                           | SSH                      

510   | abcd-epicenter.primary.ad | Guest-Router              | SNMP                      | Permit          | alw-snmp  
      | netmon.noc.xxxx.xxx       |                           |
$ 
$ 
$ 

HTH,
tyler_durden

Ty very much Tyler, that did the trick!