Solved

extract data from text file

Posted on 2011-02-21
2
420 Views
Last Modified: 2012-05-11
Hi,

I have a 30k line text file from which I need to extract and format the data as below:

Sample input:
object-group service name1 tcp
 port-object eq 9284
 port-object eq ssh
 port-object range 13701 13784
object-group service name2 tcp
 port-object range 14000 14021
 port-object eq 1908
 port-object eq ssh

There can be multiple lines below a single object-group service.
In some cases there is an additional line like description desc1 [would always start with word description]; we do not want this line; only lines starting with port-object to be listed.

Output:
application <name> term <number> protocol <protocol> destination-port <port>[-<range>]

Sample output:
application name1 term 1 protocol tcp destination-port 9284
application name1 term 2 protocol tcp destination-port ssh
application name1 term 3 protocol tcp destination-port 13701-13784

application name2 term 1 protocol tcp destination-port 14000-14021
application name2 term 2 protocol tcp destination-port 1908
application name2 term 3 protocol tcp destination-port ssh

Thank you.
0
Comment
Question by:dpk_wal
2 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 34949071
This should do what you want...
#!/usr/local/bin/perl

use strict;
use warnings;

my ($obj, $prot, $term);

my $fil = shift or die "Usage: $0 input_file\n";
open IN, $fil or die "could not open $fil: $!";
while (<IN>) {
    chomp;
    if (m{^\s*object-group\s+service\s+(.*?)\s+(\S+)\s*$}) {
        ($obj, $prot) = ($1, $2);
        $term = 1;
    } elsif (m{^\s*port-object\s+(\S+)\s+(\S+)\s*(\S*)\s*$}) {
        my $port = ($1 eq 'range') ? "$2-$3" : $2;
        print "application $obj term $term protocol $prot destination-port $port\n";
        $term++;
    } elsif (not m{^\s*description}) {
        warn "don't know what to do with line:\n$_\n";
    }
}
close IN;

Open in new window

0
 
LVL 32

Author Closing Comment

by:dpk_wal
ID: 34950653
Thank you; worked right away!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl script to parse log and delete the file 17 177
binary to char / hexadecimal 5 120
Port 80 requests 16 100
combine multiple lines 2 67
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question