1

Possible Duplicate:
How do I break out of a loop in Perl?

I have data that looks like what you see bellow. I am trying to create a perl script that will capture selected text. My idea of going about it was saying "if the previous line read was all -'s and the current line read is all ='s then stop reading the file and don't print those lines with only ='s and -'s.

However, I don't know how to code that. I only started using perl 3 days ago. I don't know if that is the best way of doing it. Let me know if there is a better way. Either way if you could help with the code, I'd appreciate it.

My code so far:

...
$end_section_flag = "true" # I was going to use this to signify 
                           # when I want to stop reading
                           # ie. when I reached the end of the
                           # data I want to capture

while (<$in-fh>)
{
    my $line = $_;
    chomp $line;

    if ($line eq $string)
    {
        print "Found it\n";
        $end_section_flag = "false";
    }

    if ($end_section_flag eq "false" )
    {
        print $out-fh "$line\n";
        // if you found the end of the section i'm reading
        // don't pring the -'s and ='s and exit
    }
}

What my data looks like

-------------------------------------------------------------------------------
===============================================================================
BLAH BLAH
===============================================================================
asdfsad
fasd
fas
df
asdf
a
\n
\n
-------------------------------------------------------------------------------
===============================================================================
BLAH BLAH
===============================================================================
...

What I want to capture

-------------------------------------------------------------------------------
===============================================================================
BLAH BLAH
===============================================================================
asdfsad
fasd
fas
df
asdf
a
\n
\n
Community
  • 1
  • 1
Ryan
  • 441
  • 1
  • 5
  • 12
  • You haven't shown any text in bold. – Borodin Jul 12 '12 at 14:58
  • [How do I break out of a loop in Perl?](http://stackoverflow.com/questions/303216/how-do-i-break-out-of-a-loop-in-perl) – matthias krull Jul 12 '12 at 14:58
  • Real world example should help. From your question is not clear what you want achieve. Show: 1.) what you have 2.) and what you want get 3.) into what variable. Your code does not have any condition about the content - the "$end_section_flag" telling nothing yet. – clt60 Jul 12 '12 at 14:58
  • 1
    http://meta.stackexchange.com/questions/18584/how-to-ask-a-smart-question – clt60 Jul 12 '12 at 15:01
  • It's not clear your idea about `-` and `=`. – dave Jul 12 '12 at 15:01
  • I've update my question to clarify. – Ryan Jul 12 '12 at 15:52

2 Answers2

1

Line-wise processing is not so suitable because your boundary crosses line endings. Slurp the file whole, then extract the in-between with the match operator.

use strictures;
use File::Slurp qw(read_file);
my $content = read_file 'so11454427.txt', { binmode => ':raw' };
my $boundary = qr'-{79} \R ={79}'msx;
my (@extract) = $content =~ /$boundary (.*?) $boundary/gmsx;
daxim
  • 39,270
  • 4
  • 65
  • 132
  • I'm new to Perl, would you mind explaining the code please? Thanks :) – Ryan Jul 12 '12 at 15:17
  • I read the file content into a string variable. Then I define the boundary, 79 dashes and some newline and 79 equals. Then I match the boundaries, and that which is in-between, against the content string. I capture the in-between and assign it to a variable. – daxim Jul 12 '12 at 16:19
0

See if this suits your needs:

 perl -ne 'm/^---/...m?/---/ and print' file

Should you want only the first block, change the delimiter from / to ? thusly:

 perl -ne 'm?^---?...m?^---? and print' file

See the range operator discussion.

This will print the range of lines bounded by '---'. You can redirect the output into a file of your choice using your shell's redirection:

perl -ne 'm/^---/...m?/---/ and print' file > myoutput
JRFerguson
  • 7,426
  • 2
  • 32
  • 36
  • I don't understand this code. Where does it go? How does it fit in? What is it doing? Sorry, I'm still new to Perl. I'd appreciate clarification. – Ryan Jul 12 '12 at 15:29