Solved

extract data from text file

Posted on 2011-02-17
6
546 Views
Last Modified: 2012-06-21
I have a 30,000 file text file from which I need to extract data; I will start with example.

Let's say I have three network object-group in the file as A,B and C with different objects as below:

object-group network C
 network-object host 10.57.31.66
 network-object host 10.57.31.67
 network-object host 10.80.40.201
 network-object host 10.80.40.202
object-group network A
 network-object 10.14.60.0 255.255.252.0
 network-object 10.14.63.0 255.255.255.0
 network-object 10.14.64.0 255.255.255.0
object-group network B
 network-object host 10.13.5.116
 network-object host 10.13.5.117


I need to extract group A and C from the file along with all other lines; i.e. line with object A and next 3 lines and then object C and next 4 lines.

Please help.
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
  • 4
6 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 300 total points
ID: 34923681
This should do it...

If you have any questions or it doesn't work as you'd like, let me know...
#!/usr/local/bin/perl

use strict;
use warnings;

# replace A and C with actual names
my %keeps = ('network A' => 1, 'network C' => 1);

# replace $file with actual filename
open IN, $file or die "could not open $file: $!";

my $keep;
while (<IN>) {
    chomp;
    if (m{^object-group\s+(.*?)\s*$}) {
        $keep = exists($keeps{$1}) ? 1 : 0;
    }
    next unless $keep;
    # replace print with whatever you want to do with the lines
    # push them into an array if you want to keep them for later
    print $_, "\n";
}

Open in new window

0
 
LVL 32

Author Comment

by:dpk_wal
ID: 34923725
Thank you for quick post; in above can we also read A and B from a text file.
The file would have entries like:
A
B
.
.
.
Z

Regards.
0
 
LVL 32

Author Comment

by:dpk_wal
ID: 34923745
Sorry for my limited knowledge with scripting, here is what I have done:
-rwxrwxrwx  1 root root 525 Feb 18 12:14 a
-rwxrwxrwx  1 root root 393 Feb 18 12:12 txtFile

Changed code above as:
#!/usr/bin/perl
open IN, $txtFile or die "could not open $txtFile: $!";

Getting error as below:
[root@myhost]# ./a
Global symbol "$txtFile" requires explicit package name at ./a line 10.
Global symbol "$txtFile" requires explicit package name at ./a line 10.
Execution of ./a aborted due to compilation errors.

Thank you for all your help.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Assisted Solution

by:ozo
ozo earned 200 total points
ID: 34923853
open IN, "<txtFile" or die "could not open txtFile: $!";
0
 
LVL 32

Author Comment

by:dpk_wal
ID: 34924039
Thank you for all the help.
0
 
LVL 32

Author Closing Comment

by:dpk_wal
ID: 34924046
Thank you!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

696 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