Solved

extract data from text file

Posted on 2011-02-21
2
413 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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 173
HTTP Error 502.2 - Bad Gateway 3 212
Strange perl issue 6 128
Log File Creation with Header and Footer 17 92
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

813 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

10 Experts available now in Live!

Get 1:1 Help Now