user1001853
user1001853

Reputation: 1

How do I extract specific data from a .txt file in perl

I have some text file which have data in the following format:

Summary:
xyz

Configuration:
abc
123

Tools:
pqr
456

The tags 'Summary:', 'Configuration:' and 'Tools:' remain the same for all the files and just the content below that changes. I need to extract just the content from the text file and print it out.

Thanks.

Upvotes: 0

Views: 563

Answers (4)

PinkElephantsOnParade
PinkElephantsOnParade

Reputation: 6592

Or the considerably less elegant

#!/usr/bin/perl
my $fi;
my $line;
my @arr;
open($fi, "< tester.txt") or die "$!";

while ($line = <$fi>) 
{
    if (!($line =~ /:/))
    {
        push(@arr, $line);
    }
}

foreach (@arr)
{
    print $_;
}

close ($fi);
close ($fo);

Upvotes: -1

Stephen Gross
Stephen Gross

Reputation: 5714

How about something like:

open(FH, '<myfile.txt');
while(<FH>)
{
  print $_ unless /Summary:|Configuration:|Tools:/;
}

You'll have to cleanup the regex a bit, but that should do it.

Upvotes: 1

Meng Lu
Meng Lu

Reputation: 14637

Not sure if you have one file or many, but if the former case:

Summary: xyz

Configuration: abc 123

Tools: pqr 456

Summary: xyzxyz

Configuration: abcd 1234

Tools: pqr 457

You can use something like the following to print all configuration lines:

#!/usr/bin/env perl

open (FILE, 'data.txt') or die "$!";

while (<FILE>) {
    if (m/^(Configuration):(.*)/) {
      print $2 . "\n";
    }
}
close FILE;

Change the regex if you want other lines or to m/^(Configuration|Tools|Summary):(.*)/ for all of them.

Upvotes: 0

TLP
TLP

Reputation: 67908

use strict;
use warnings;

my (%h, $header);
while (<>) {
    if (/^(\w+):) { 
        $header = $1; 
    } elsif (defined $header) { 
        push @{$h{header}}, $_; }
}
print Dumper \%h;

If your non-header lines can contain :, you may need something stricter, such as:

if (/^(Summary|Configuration|Tools):/)

Upvotes: 1

Related Questions