Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Extract lines between two patterns

by mnithink (Initiate)
on Jan 22, 2012 at 06:36 UTC ( [id://949225]=perlquestion: print w/replies, xml ) Need Help??

mnithink has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl-experts, I am new to perl and need help to solve a problem. I have a table in below format.
<Text A> <Pattern1> A Value B Value C Value D Value <Pattern2> <Text B>
This table is in file1. I want to extract lines between Pattern1 and Pattern2 and write it into file2. Please provide me some help to do this. Thanks

Replies are listed 'Best First'.
Re: Extract lines between two patterns
by GrandFather (Saint) on Jan 22, 2012 at 07:04 UTC

    What have you tried and how did it fail? We can help you much better if we can see your code, and better still if you can provide a self contained sample script along with your description. See I know what I mean. Why don't you? for some guidance.

    True laziness is hard work
      Thanks for the reply First I experimented with this below code on a single file.
      perl -ne 'print if /Pattern1/ .. /Pattern2/' file1
      This worked for me. But my actual intent is to do this in a loop on multiple files. My code looks as below
      #!/usr/bin/perl use strict; use warnings; our $fh; our $log; open($fh,">","file1"); open ($log,">","file2") or die "can not open file $log : $!\n" ; while($fh){ if (/Pattern1 .. /Pattern2/) {printf $log;} }
      when i execute this code, i'm getting below message
      "Use of uninitialized value in pattern match (m//) at temp.pl line 11. +"

        There is nothing much wrong with the code you have posted (with the provisos mentioned below) but the problem appears to be in the pattern(s) in your if statement. The error message indicates that you are using some undefined variable in one or other of the pattern matches in that statement. However, you don't have any variables within the patterns shown--perhaps you should post the actual code?

        A couple of other comments: please put your code in <code> tags--this would make it much more readable. You would probably be better to use lexical (my) rather than package (our) variables in your use case. Finally, you are missing a terminating "/" in the first pattern match, presumably because you have retyped a shortened version of your code. There is nothing wrong with showing PerlMonks a shortened version of your code--in fact it is encouraged--but it needs to be runnable, and demonstrate the bug.

        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Extract lines between two patterns
by Anonymous Monk on Oct 16, 2012 at 12:54 UTC
    so a different way to do this is with a flag and What I did was use the $on as a flag to see when to stop printing and to start so hope this helps you! if anyone knows how to a prompt string in the find part: where you search for the word you want to start from and end at if anyone can give me that then thank you! also the die thing i was told that you can have it on the same line as the open I'm relatively new to Perl so if you can also give me some pointers and shortcuts.
    #!/\\perl # use strict; use warnings; # Put the file name in a string variable # so we can use it both to open the file # and to refer to in an error message # if needed. my $fileName ; my $success ; my $on = 0; # Hard-code the name of the file we're going to read. # This is the script itself. $fileName = '/\\rat09awp\ccscripts\Islam\test.log'; # Attempt to open the file. We create a file handle called "IN". # Open returns a true value if it succeeded, an undefined # value if it didn't succeed. $success = open( IN, "<$fileName" ); while ( <IN> ){ if ($_ =~ /Duplicate remedy_id:/){ print $_; $on = 1; } elsif($on){ if ($_ =~ /Duplicate ip_address:/) { last; } else{ print $_;} } # We have read all the lines from the file. # Close the file. } close( IN );

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://949225]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-25 08:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found