Solved

extract data from text file

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTTP Error 502.2 - Bad Gateway 3 205
perl CPAN issue 3 99
Need Helping Editing Perl Script for PowerChute Network Shutdown 3 77
Perl File::Find alternative 1 52
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.…
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…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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