Excellent; some code to work with.

Okay, some basic coding structure issues first (and a point or two on style; I'm sorry, I can't help myself). Then we address your issue.

  1. Reformatted for readability. Please download the attached CODE from the bottom of this node. I will be using those line numbers, not your original ones, so please use it to follow along.
  2. Line 3: I strongly recommend you  use strict;  always. It will save you a LOT of hassle. With that one line of code, you just hired your Perl interpretter as a debugger, and it even works for free. It will seem like a husband or wife, continually finding fault with your work. But it will almost always be right, and you will become a better Perl programmer much more quickly.
  3. Line 20: You do  opendir DIR .
    Line 23: You do  close DIR .
    You have made this mistake in previous posts.  open  and  close  are matched;  opendir  and  closedir  are matched.

    It's sort of like getting into your car through the window. You can open the window, and then close the window. Or you can break the window, and then repair the window. You don't open the window and then repair the window. These things are matched sets, and they need to be used as matched sets.

    Side note: Stealing code (especially your own) is a time-honored tradition. You get something that works, you save it, and you re-use it to save yourself time and trouble.

    However, since you made this exact same error in a previous script posted here, it seems you have re-used a broken piece of code instead of a working piece of code. You might consider adjustments to your process to reduce or eliminate the threat of this error in the future.
    Might save you some time and hassle.

  4. Line 29: You do  open filein,$filer; 
    It is common to make the bareword file handle in all caps so it stands out. Not required syntactically, but adviseable for readability.
    I'd do:  open FILEIN,$filer; 
    Note: If you do this, remember you'll have to change all your references from  filein  to  FILEIN .
  5. Line 29: You open a file here but never close it.
  6. Line 36: Again, I'd uppercase the file handle.
  7. Line 36: You open a file here but never close it.
  8. Line 36: You claim the file you cannot open is called  $out , but there is no  $out . You would probably have caught this if you'd been using  use strict; .
  9. Line 50: You do  close $out; . But you never opened it. You would probably have caught this if you'd been using  use strict; .
  10. Line 32: Here is the crux of your problem, once you clean up the rest of your script. Does this regular expression even work? I'm no expert on regular expressions, but this looks funky to me (which is humorous, given that regular expressions generally look funky anyway, but I digress).

    I'd write a test script to confirm how the regular expression will function. Sample:

    #!/usr/bin/perl -w use strict; my @TestInput = ( 'Line One', 'Line Two', 'Line Three', 'Line Four', 'Line Five', ); foreach my $testLine (@TestInput) { print "-----> '$testLine'\n"; if(/(\d\_)+a1/.../(\d\_)+a1/) { my $var3 = "$outdir/$1 +to+ $2.txt"; print " Found '$var3'\n"; } } exit;

With your code structure repaired,  use strict;  at your side, and the behavior of your regular expression confirmed with the test script, see if you can make the code work.

And we'll all be here if you get stuck again. Just show us what you tried.

The reformatted script:

#!/usr/bin/perl -w use strict; { print"enter the input directory path:\n"; chomp($indir=<STDIN>); print"enter the output directory name:\n"; chomp($outdir=<STDIN>); if ($indir eq $outdir) { print"you cannot have same input and ouput directory please +change:\n"; exit(); } else { chdir ("$indir") or die "$!"; opendir(DIR,".") or die "$!"; my @files=readdir DIR; print @files; close DIR; foreach $file(@files) { unless (($file eq ".") || ($file eq "..") ) { $filer="$indir/$file"; open filein,$filer; while (<filein>) { if(/(\d\_)+a1/.../(\d\_)+a1/) { print; $var3="$outdir/$1 +to+ $2.txt"; open filew,">>$var3" or die "cannot open + $out:$!"; print filew $_,"\n"; } } } } print"<----------------------------------------------->\n"; print "\t\t action done\n"; print "\a"; print "\a"; print "\a"; print"<----------------------------------------------->\n"; print"Results could be found in $outdir as txt files with TC + name\n"; print"<**********************..............***************** +********>>\n"; close $out; } } exit;


In reply to Re^3: extraction of text between 2 similar patterns in a text file by marinersk
in thread extraction of text between 2 similar patterns in a text file by seek_m

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.