?
Solved

extract data from text file

Posted on 2011-02-21
2
Medium Priority
?
435 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 2000 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Six Sigma Control Plans

762 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