Re: Parsing multi-line text
- From: "Peter J. Holzer" <hjp-usenet2@xxxxxx>
- Date: Sat, 1 Mar 2008 16:42:00 +0100
On 2008-02-18 10:42, keith@xxxxxxxxxxxxxxxxxx <keith@xxxxxxxxxxxxxxxxxx> wrote:
I have a data file structured something like this:
------------------8<-----------------------
Chunk 01
NAME: "Alice"
Description: "Some other string"
Age: 37
Chunk 02
NAME: "Bob"
Description: "Some other string"
Age: 28
Chunk 03
FIRST: "Carol"
Description: "Some other string"
Age: 32
Chunk 04
FIRST: "Dave"
Description: "Some other string"
Age: 22
------------------8<-----------------------
and I want to extract from it to produce output something like this:
------------------8<-----------------------
01 NAME: Alice -> 37
02 NAME: Bob -> 28
03 NAME: Carol -> 32
04 NAME: Dave -> 22
------------------8<-----------------------
Sure about that? In the input you have sometimes "FIRST" and sometimes
"NAME", but in the output it is always NAME. Assuming this is
intentional:
#!/usr/bin/perl
use strict;
use warnings;
my $s = <<EOS;
Chunk 01
NAME: "Alice"
Description: "Some other string"
Age: 37
Chunk 02
NAME: "Bob"
Description: "Some other string"
Age: 28
Chunk 03
FIRST: "Carol"
Description: "Some other string"
Age: 32
Chunk 04
FIRST: "Dave"
Description: "Some other string"
Age: 22
EOS
while ($s =~ m{
^Chunk \s (\d+) \n
\s+(NAME|FIRST): \s "(.*?)" \n
\s+Description: \s "(.*?)" \n
\s+Age: \s (\d+) \n
}xmg
) {
print "$1 NAME: $3 -> $5\n";
}
hp
.
- Prev by Date: Re: FAQ 4.2 Why is int() broken?
- Next by Date: Tecnique and/or Modules for Perl and HTML
- Previous by thread: Re: FAQ 4.2 Why is int() broken?
- Next by thread: Re: Parsing multi-line text
- Index(es):
Relevant Pages
|