Solved

extract data from text file

Posted on 2011-02-21
2
395 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now