Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

log file in perl.

by manu_06 (Novice)
on Aug 27, 2010 at 18:44 UTC ( [id://857743]=perlquestion: print w/replies, xml ) Need Help??

manu_06 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

How to generate a flat file using perl. I mean right now when my script runs a log file will be created in this way.

abcd 0002 0003..
But I want it to print in the following format.
abcd | 0001 | 0002 |...
so that i can read that file into the database tables. Thanks,

Replies are listed 'Best First'.
Re: log file in perl.
by kennethk (Abbot) on Aug 27, 2010 at 18:52 UTC
    What does your code look like now? We need to see how you are getting the incorrect output to tell you how to correct it. See How do I post a question effectively?.

    Assuming your code looks similar to what you posted in Perl Script to count files and directories is not working., the easiest solution for you would be to not put new-lines in your print statements (\n). So for example, line 28 in that OP might change from

    print "Directories: $directory_count\n";

    to

    print "Directories: $directory_count | ";

    Of course, you could do this a large number of different ways. You could also presumably alter the way you "read that file into the database tables". TIMTOWTDI.

      Hi,

      Thank you and its working now. If possible can I know how to tweak my script to get filenames, date and time in addition to counts.

      #!/usr/local/bin/perl my $dir= ('c:\My Projects\Perl Scripts\New Folder') ; $directory_count = 0; $file_count=0; $outfile = 'log.txt'; open my $OUTF, "> $outfile" or die print " can't create logfile; $!"; opendir( DIR, "$dir" ) ; my @files = readdir( DIR ) ; foreach $file ( @files ) { if ( -f "$dir/$file") { $directory_count++; } else { $file_count++; } } print {$OUTF) "Directories: $directory_count |"; print ($OUTF) "Files: $file_count\n"; closedir( DIR ) ; } close OUTF ;
        What's stopping you? The answer to your question lies in you trying to understand the code you have. You already have the file name in your $file variable. You can get information on files using the -X family of operators, but you should know that already since your code contains -f. stat might also be helpful.

        I find it odd that you say it's working now, as I took your posted code, attempted to run it, and obtained the errors:

        String found where operator expected at fluff.pl line 35, near ") "Dir +ectories: $directory_count |"" (Missing operator before "Directories: $directory_count |"?) print (...) interpreted as function at fluff.pl line 36. String found where operator expected at fluff.pl line 36, near ") "Fil +es: $file_count\n"" (Missing operator before "Files: $file_count\n"?) syntax error at fluff.pl line 35, near "$OUTF) " syntax error at fluff.pl line 36, near ") "Files: $file_count\n"" Execution of fluff.pl aborted due to compilation errors.

        I can also spot a number of other issues that would be caught by strict and warnings, as you claim to have implemented Re^2: Perl Script to count files and directories is not working.. As an internet programming forum, PerlMonks is ridiculously respectful and helpful to the neophyte. We do not have patience for people who show no effort. I highly recommend you read How To Ask Questions The Smart Way and meditate on it before posting again.

        You've already got the filenames in your foreach loop. Each iteration of $file has the filename.

        For dates/times of files, have a peek at stat

Re: log file in perl.
by sundialsvc4 (Abbot) on Aug 30, 2010 at 15:58 UTC

    Echoing the first responder ... I personally would write the program so that it validated the log-file and loaded it into the database, instead of preparing an output file and then relying upon a third-party program (even a vendor-supplied one) to do it.   There’s just too much uncertainty in doing it the other way:   “the programs might all run to completion, but how do you know that the process actually worked?”

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://857743]
Approved by kennethk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-23 11:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found