简体   繁体   中英

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.

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.

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):/)

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM