Dear monks,
i have a task to open a directory and read thru some 100 or 150 text files in it line by line and substitute a value for each .when i read thru half way of the 150 txt files i get a
permission denied at xx.pl line xx error and the script terminates ubruptly. i even tried using flock,but no use.
Please help me on this?
Thanks,
seeker
this is my code
use File::Basename;
use Fcntl qw(:flock);
print"enter the input directory path:\n";
chomp($indir=<STDIN>);
print"enter the events directory path:\n";
chomp($evdir=<STDIN>);
print"enter the output directory name:\n";
chomp($tmpdir=<>);
if (($indir eq $outdir)||($tmpdir eq $outdir)||($indir eq $tmpdir))
{
print"you cannot have same input and ouput directory please change:\n"
+;
exit();
}
else
{
$ever="$evdir/events.txt";
chdir ("$indir") or die "$!";
opendir(DIR,".") or die "$!";
my @files=readdir DIR;
close DIR;
my %result=();
foreach $file(@files)
{
unless (($file eq ".") || ($file eq "..") )
{
$base=basename("$file",".txt");
$filein="$indir/$file";
$srtfile="$outdir/$base._events.txt";
$tmpout="$tmpdir/$base._unsortedevents.txt";
open evein,$ever or die $!;
flock(evein, LOCK_EX) or die "Cannot lock - $!";
while (<evein>)
{
chomp;
next unless length;
my ($key,$value)=split /===/,$_;
$result{$key}=$value;
}
close(evein);
while (my($key,$value)=each (%result))
{
open fln,$filein or die $!;
flock(fln, LOCK_EX) or die "Cannot lock - $!";
while(<fln>)
{
my ($line)=$_;
chomp($line);
if ($line=~m/$key/ig)
{
$line=~s/$key/$value/eeig;
open flow,">>$tmpout" or die $!; #prints matching log lines in events.
flock(flow,LOCK_EX) or die "Cannot lock - $!";
print flow $line,"\n";
}
}
print $value,"\n";
close(flow);
}
}
}
print"<----------------------------------------------->\n";
print "\t\t action done\n";
print"<----------------------------------------------->\n";
print "\t\t$count results found in search\n";
print"<****************..............******************>>\n";
close $out;
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.