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.
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.
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |