Then, please clarify what your sample output is telling us: on first reading I thought you wanted one output file with all the material except the "start" lines... but, on rereading, the phrase "inside a text file 2. and so on" (belatedly) caught my eye, making me wonder if you wanted each instance of the boldfaced material to go in a separate file (and no, I didn't bother to try to answer that question by studying the code you posted: it's not easily readable and less than readily comprehensible for reasons outlined by others).
But, the Perl slogan "There is more than one way to do it" (aka TIMTOWTDI or TIMTOADY, etc)" is definitely a truism for your problem, however one is supposed to understand your question.
#!/usr/bin/perl use warnings; use strict; use 5.012; # 905094 my @data = <DATA>; my $data; my $file = "f:/_wo/905094.txt"; open ( OUT, "> $file" ) or die "Can't open FH for write, $!"; for $data(@data) { chomp $data; if ($data !~ /start/ ) { # say $data; say OUT $data . "\t|"; } } say "done"; =prints to the file: this is a example line | | this is a example 1 line | | this is a example 2 line | | this is a example 3 line | | =cut __DATA__ start this is a example line start this is a example 1 line start this is a example 2 line start this is a example 3 line start
and there are more.
This site, including Super Search and Tutorials can help you get started with Perl (yes, I suspect the reply code is cargo-culted from somewhere without much understanding). So too can numerous on-line tutorials (for example, those at http://learn.perl.org/) but search this site a bid to see which are well-regarded and which are trash) and, finally, being very much "old-school," I also suggest ( tada!! ) "books" such as Learning Perl.
Addendum:
Your title, if carefully chosen, suggests you are thinking of the "this is a example \d line" as the "similar patterns," perhaps because those are what you want to capture. But for your purposes, think instead of what you want to discard; instances of "stop" (with or without blank lines preceding).
It's easy enough to write a regex to deal with the "this is..." lines -- matching on any digit and capturing work just fine (when the regex is constructed correctly), but there's a lot less opportunity for error (in your example data) to match on the four letter word, "stop."
In reply to Re: extraction of text between 2 similar patterns in a text file
by ww
in thread extraction of text between 2 similar patterns in a text file
by seek_m
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |