Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How to read each line of file into an array and split contnts of each line into array cells

by crazy-duck (Novice)
on Sep 30, 2013 at 05:11 UTC ( [id://1056279]=perlquestion: print w/replies, xml ) Need Help??

crazy-duck has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perlmonks,

Could please help me with the below situation... we have a logfile with below form of entries

**FAILED PROD** - BUGS - 08/26/13 @11:12 - BUGS~23AUG13~23AUG13~23AUG13~26AUG13~1109

**FAILED PROD** - cockroach - 08/29/13 @11:52 -cockroach~29AUG13~29AUG13~29AUG13~29AUG13~1152

we need to capture only those logfile entries that come between 6pm to 6am following day. Iam trying to read each line of the logfile into an array and again split components of each line into an another array to compare the time stamp between 18hrs to 6hrs. We need to read new entries in the logfile only Iam new to Perl, can any help on this.

Thank you cd
  • Comment on How to read each line of file into an array and split contnts of each line into array cells

Replies are listed 'Best First'.
Re: How to read each line of file into an array and split contnts of each line into array cells
by NetWallah (Canon) on Sep 30, 2013 at 06:28 UTC
    Welcome to the monastery.

    The following code probably does what you want, and more, but will you learn more if you follow " How do I post a question effectively?".

    perl -F-\|@\|~ -anE 'say for @F; push @x, {TIME=>$F[3],REC=>$_,PIECES= +>[@F]}}{for (@x){next unless $_->{TIME} ge "06" and $_->{TIME} le "1 +8";say qq(do someting with $_->{REC}) }' <Your file name>

                 My goal ... to kill off the slow brain cells that are holding me back from synergizing my knowledge of vertically integrated mobile platforms in local cloud-based content management system datafication.

Re: How to read each line of file into an array and split contnts of each line into array cells
by CountZero (Bishop) on Sep 30, 2013 at 06:19 UTC
    Show us what you have tried (even if it failed).

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      #!/usr/bin/perl; use strict; use warnings; use Getopt::Long; my @ARRAY = (); my $ARRAYlength = -1; my $FILEname="/var/log/logfile.txt"; open FILE, $FILEname or die "couldn't open $FILEname: $!"; while (<FILE>) { @ARRAY = <FILE>; print "@ARRAY\n" } close FILE or die "problems closing $FILEname: $!";,<

      Output of above script: **FAILED PROD** - BUGS - 08/26/13 @11:12 - BUGS~23AUG13~23AUG13~23AUG13~26AUG13~1109

      **FAILED PROD** - cockroach - 08/29/13 @11:52 -cockroach~29AUG13~29AUG13~29AUG13~29AUG13~1152

      Iam able to read the logfile lines into array and at this point I want to split components of each line and check the time and alert if it is between 18hrs to 6AM following day.

Re: How to read each line of file into an array and split contnts of each line into array cells
by wjw (Priest) on Sep 30, 2013 at 06:34 UTC
    I would guess you can split the lines without help as that is very basic.

    Once you have done that, look at Date::Calc to help you determine the appropriateness of any given entry. Your date field will need to be moderately massaged to work, but that is also very basic.

    Just look at Date::Calc on CPAN and read it... all will become apparent...



    • ...the majority is always wrong, and always the last to know about it...
    • ..by my will, and by will alone.. I set my mind in motion
Re: How to read each line of file into an array and split contnts of each line into array cells
by hdb (Monsignor) on Sep 30, 2013 at 11:06 UTC

    You do not really need to split the lines to find the time. Extract it with a regex and then print the line unless the time is within 6 hours either side of noon:

    use strict; use warnings; abs( 1200 - join "", /(\d\d):(\d\d)/ ) < 600 or print while(<DATA>); __DATA__ **FAILED PROD** - BUGS - 08/26/13 @05:12 - BUGS~23AUG13~23AUG13~23AUG1 +3~26AUG13~ **FAILED PROD** - BUGS - 08/26/13 @11:12 - BUGS~23AUG13~23AUG13~23AUG1 +3~26AUG13~1109 **FAILED PROD** - cockroach - 08/29/13 @11:52 -cockroach~29AUG13~29AUG +13~29AUG13~29AUG13~1152 **FAILED PROD** - cockroach - 08/29/13 @18:00 -cockroach~29AUG13~29AUG +13~29AUG13~29AUG13~1152

      "...You do not really need to split the lines to find the time. Extract it with a regex and then print the line..."

      And split doesn't use regex shea? of course I know it does uses

        In my response, there is no reference to Perl's split. Besides being a popular function in Perl, split is also an English verb. Most likely split got its name from split. Now, splitting strings into parts in Perl is often done using split but can be achieved in numerous other ways. All I wanted to say is, that the retrieval of the time is possible by applying a regular expression to the complete line from the log file without splitting it first.

        split might use regex. The statement is still correct: You don't need to use split.
Re: How to read each line of file into an array and split contnts of each line into array cells
by kcott (Archbishop) on Sep 30, 2013 at 13:30 UTC

    G'day crazy-duck,

    Welcome to the monastery.

    You should set up dummy data to test your solution. That data should include boundary conditions (e.g exactly 6am, 1 minute before, and 1 minute after). You also need to be clearer about the range: "between 6pm to 6am" inclusive? exclusive? other?

    In the code you posted, there's some problems:

    • See the open documentation for the preferred way to open a file for reading. Something like: open my $fh, '<', $filename ...
    • while (<FILE>) (which would become while (<$fh>)) reads a record at a time. Given you're reading a (potentially very long) log file, this is a good idea.
    • After reading the first record, you read all the remaining records into an array with @ARRAY = <FILE>. This is a bad idea for the same reason that reading a record at a time was a good idea. Also, as there's now no more records to read, the while loop won't reiterate (i.e. in total, it only loops once).
    • There's some garbage in the close FILE line. Possibly a typo; however, you need to post exactly the code you're running: we can't guess what you meant to write. See the close documentation, if necessary.

    Identifying the lines you want is fairly straightforward. For something this simple, I wouldn't recommend a CPAN module nor a complex calculation. A plain comparison, which is easy to read and, if necessary, easy to adjust, is all that's required.

    #!/usr/bin/env perl use strict; use warnings; while (<DATA>) { my ($time, $hours, $mins) = /@((\d\d):(\d\d))/; next unless $hours < 6 || $hours > 17 || $time eq '06:00'; # A line we want is in $_ - process it here, e.g. print; } __DATA__ **FAILED PROD** - BUGS - 08/26/13 @00:00 - ... WANTED 1 **FAILED PROD** - BUGS - 08/26/13 @00:01 - ... WANTED 2 **FAILED PROD** - BUGS - 08/26/13 @05:59 - ... WANTED 3 **FAILED PROD** - BUGS - 08/26/13 @06:00 - ... WANTED 4 **FAILED PROD** - BUGS - 08/26/13 @06:01 - ... NOT WANTED 1 **FAILED PROD** - BUGS - 08/26/13 @12:00 - ... NOT WANTED 2 **FAILED PROD** - BUGS - 08/26/13 @17:59 - ... NOT WANTED 3 **FAILED PROD** - BUGS - 08/26/13 @18:00 - ... WANTED 5 **FAILED PROD** - BUGS - 08/26/13 @18:01 - ... WANTED 6 **FAILED PROD** - BUGS - 08/26/13 @23:59 - ... WANTED 7 **FAILED PROD** - BUGS - 08/26/13 @24:00 - ... WANTED 8 (LAST)

    Output:

    **FAILED PROD** - BUGS - 08/26/13 @00:00 - ... WANTED 1 **FAILED PROD** - BUGS - 08/26/13 @00:01 - ... WANTED 2 **FAILED PROD** - BUGS - 08/26/13 @05:59 - ... WANTED 3 **FAILED PROD** - BUGS - 08/26/13 @06:00 - ... WANTED 4 **FAILED PROD** - BUGS - 08/26/13 @18:00 - ... WANTED 5 **FAILED PROD** - BUGS - 08/26/13 @18:01 - ... WANTED 6 **FAILED PROD** - BUGS - 08/26/13 @23:59 - ... WANTED 7 **FAILED PROD** - BUGS - 08/26/13 @24:00 - ... WANTED 8 (LAST)

    -- Ken

      Hi Ken

      Thank you for your response and appreciate all your efforts in helping here, for the above script can we amend to do a text comparision "**FAILED PROD**" that comes in between 6pm to 6am inclusive.

      Thank you cd
        "Thank you for your response and appreciate all your efforts in helping here,"

        You're welcome.

        "for the above script can we amend to do a text comparision "**FAILED PROD**" that comes in between 6pm to 6am inclusive."

        I don't understand this:

        • I don't know who you are referring to when you say "we".
        • If you're asking for some sort of permission to use/modify this code, you don't need to.
        • The code is already dealing with "6pm to 6am inclusive".

        -- Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-19 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found