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

Ok, so i'm basically brand new to Perl, my boss has asked me to put together a perl script which will parse logs into a CSV file in a table format but i have no idea where to start to be completely honest ... so far i've managed to come up with this as my objective to be reached read the log file(s) then put them into a csv file with a table which looks a little like this

Drive, Started, Completed, Size, Time

monday tuesday wednesday thursday friday the days of the week are in one column

could someone please help or point me in the right direction i have read a few tutorials but i just don't seem to be able to understand it as it is my first time of actual programming/coding

Thanks in advance

Replies are listed 'Best First'.
Re: Hi all, need abit of help!
by BrowserUk (Patriarch) on Jul 11, 2011 at 22:32 UTC
    but i have no idea where to start

    Start at the beginning. And start simple.

    Your spec calls for reading one file and writing to another. So write a script that reads lines from one file and writes them to new file, without changing anything for now.

    When you've got that working, come back and post your code and we can guide you onto the next step.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Hi all, need abit of help!
by SuicideJunkie (Vicar) on Jul 11, 2011 at 23:02 UTC

    The first thing to do is ask yourself how you would write down instructions for a person to do it:

    1. Read the log file... one entry at a time
    2. For each entry,
      1. Fill in the table with the info from the entry.
      2. Figure out what day of the week the entry is talking about, and put a check in the matching column.

    Next pretend that this person knows absolutely nothing about the business, and is an idiot savant that only knows math well. Also, they don't understand English except for your instructions.

    Reading the log file needs to be broken down: how do you identify a record? Which strange symbols are the start/end? (Likely a newline (\n) character, or perhaps a line with the symbols "DRIVE BACKUP START" on it then one with the symbols "DRIVE BACKUP FINISHED" on it.)

    Regular expressions help a lot here, but reading a line at a time and skipping the irrelevant ones may be good enough.

    Once you've found a bunch of symbols that constitute a record, you need to say how to find the elements of it you care about. How do you know what the "drive" is? (First "Space, a-z then colon" in the record?) Where is the start time, and so on.

    Regular expressions usually help even more for this.

    Once you've found and picked out the info you want, you need to write it out into the CSV. Writing a CSV like that, you'll want to open a new file and write out the header line before you start looking at records.

    Since there shouldn't be any commas in the data, you shouldn't need any fancy stuff for escape characters and quoting rules. So, you want to just copy out all the info symbols in the order you want with commas in between.

    Write down all those very simple steps using #comments in your .pl file, and then slowly fill in with perl code after each one.

    Compile and run often to make sure you're still on the right track. Once you get to the end, you're done! :)


    PS:

    You'll want to bookmark http://perldoc.perl.org for looking up all the basics and syntax. (If your browser window isn't exactly 950 pixels wide, try the Stylish addon for firefox with http://userstyles.org/styles/20328 to make the page render with a dynamic width.)

    Also, be sure to use strict; use warnings; at the top of your program. It is best to hear about the problems in your code as soon as possible, rather than having things go mysteriously wrong. I also recommend running the program often, after adding each little piece of code. That way when it starts failing to compile, you know right where the bug is. :)

Re: Hi all, need abit of help!
by ig (Vicar) on Jul 12, 2011 at 06:22 UTC

    As an alternative to learning to program and learning Perl, you might step back to see if anyone has already solved the problem. If the logs are produced by a commonly used program, you may find that someone has already written a program to parse them and produce reports that would meet your needs.

    As for learning Perl, you can find pointers to many good resources covering Perl generally in Getting Started with Perl.

    Learning programming is not the same thing as learning Perl. It will take more than a few weeks to learn programming, if you have never done it before. Taking an introductory course may be a good place to start, at your local college or university.