REGEX matching and extraction

I have a file consisting of many set of values. I am trying to just extract the required lines that matched in a file once a REGEX is found. File.txt

    ...... 
    ......
    TRIP (
            ?name           "model"
            ?prompt         "Model name"
            ?defamount       "USD100"
            ?type           "adventure"
            ?display        "no"
            ?photos       "800"
               )
    TRIP (
            ?name           "macro"
            ?prompt         "macro model name"
            ?defamount       "USD500"
            ?type           "adventure"
            ?display        "no"
            ?photos       "1200"
            )
      TRIP( 
            ?name           "description"
            ?prompt         "description"
            ?defamount       "USD400"
            ?type           "historical"
            ?display        "yes"
            ?photos       "900"
             )
            .......
........
.....
.......
......

I want to extract the line with "name and model" then "name and description" along with these line i want to extract the 1st "defamount" once the the line with "name and model" then "name and description" are matched. I tried with loops but i wasnt succesful

use strict;
open FILE, "<File.txt";
open FILE2, ">>data1.l";

while (my $string = <FILE>) {

    if ($string =~ m/^ CELL/ig) {

        print FILE2 $string;
    }
    elsif ($string =~ m/"model"/i) {

        print FILE2 $string;
    }
    elsif ($string =~ m/defamount/o) {

        print FILE2 $string;
    }
    elsif($string =~ m/"description"/i) {

        print FILE2 $string;
    }
    elsif($string =~ m/defamount/o) {

        print FILE2 $string;
    }
}
close FILE;
close FILE2; 

This gives me all the "defamount" lines from the file, but i want just the 1st defamount after the matching the above mentioned regex

Answers


Each of those if...elsif constructions are calculated on every line of the input. You need to treat them as groups of lines, and hence keep track of whether you've already matched a line to decide if this is a group you want to keep or not.

Without completely rewriting your program, you need to do something like this:

my $keep = 0;

while(my $string = <FILE>){
    if ($string =~ m/name\s+/){ #matches name, start of new group
        if ($string =~ m/"(model|description)"/){ # keep this group
            $keep = 1;
            print FILE2 $string;
        } else { #discard this group
            $keep = 0;
        }
    }
    if ($string =~ m/defamount/ && $keep){ #only print if in a 'keep' block
        print FILE2 $string;
    }
}

To be honest, I would write this quite differently if I was starting from scratch. That's some pretty agricultural perl you've got going on there. But hopefully the logic of what I've written will help you get going.


Need Your Help

MVC Multi User Authentication/Security

php security codeigniter authentication

I've been working on a web application for a company that assists them with quoting, managing inventory, and running jobs. We believe the app will be useful to other companies in the industry, but

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.