It seems like there are so many problems with the OP code that the replies so far simply haven't been able to cover them all. There are still a couple whoppers that no one has pointed out yet. The first OP snippet starts out like this:
$dir= shift || '.'; opendir DIR, $dir or die "Can't open directory $dir: $!\n"; while ($file= readdir DIR) { next if $file=~/^\./; open (FH, ">", "$file") or die "Can't open file $file: $!\n"; my @file_lines=<FH>;
You truncate/open a file for output and then try to read from it? I hope you made a backup of the directory before you ran that script with $dir set to "." because the script would have obliterated all the data files. (Running it with some directory name in @ARGV would have saved your input data from oblivion, but wouldn't have gotten anything done.)

The second snippet avoids that problem, but still shares another problem with the first version: if $dir is set to something other than "." (i.e. via @ARGV), the open statement would need to be like this in order to do what you want:

open (FH, "$dir/$file") ...
Luckily, when you do it that way, it still works when $dir is set to "."

Put all that together with the other replies, and you should get pretty close to a working script.


In reply to Re: parsing a directory of log files. by graff
in thread parsing a directory of log files. by learn2earn

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.