Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


extract data from text file

Posted on 2011-02-17
Medium Priority
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
 network-object host
 network-object host
 network-object host
object-group network A
object-group network B
 network-object host
 network-object host

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.
Question by:dpk_wal
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
LVL 27

Accepted Solution

wilcoxon earned 1200 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...

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>) {
    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

LVL 32

Author Comment

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:

LVL 32

Author Comment

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:
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.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 84

Assisted Solution

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

Author Comment

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

Author Closing Comment

ID: 34924046
Thank you!

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…

618 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