Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: DateTime::Format::Flexible; for Log Parse with multiple formatted lines

by 1nickt (Canon)
on Mar 23, 2017 at 19:12 UTC ( [id://1185665]=note: print w/replies, xml ) Need Help??


in reply to DateTime::Format::Flexible; for Log Parse with multiple formatted lines

It appears that you are not only reading in all the lines of the file at once, but then combining them all into one string. This is probably not a scalable solution.

Instead consider using while to go through the lines one at a time, processing the text and extracting the data you need in a loop.

The other thing I saw at a glance is that you are trying to use $0 as a regex capture, which will not do what you expect.

Edit: Also please place your sample input into <code></code> tags, as it's not rendering accurately at the moment and can't be used for testing. Please consider sharing an SSCCE which in this case would include a couple of sample lines in the __DATA__ section, the regexp you're using to extract the fields, and then the date handling code ... all in one script of 20 - 30 lines.


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: DateTime::Format::Flexible; for Log Parse with multiple formatted lines
by TCLion (Novice) on Mar 23, 2017 at 19:48 UTC

    I did use the code tags. it works fine on my browser, just small window and the download link works on all of them.
    So instead of one continuous string how would you use while for this code in a loop?
    I am not sure what you are asking for, for SSCCE, I did what I believe I was told for the SoPW and I did give all code (properly as I understand it) and example lines, What am I missing/not understanding.
    Where is a data section?
    Part of my problem is I am unsure of the date handling code due to DateTime::Format::Flexible; is new to me and I have not understood the documentation completely which is why I am asking in the first place. Please don't take this as me being an ass. I just don't understand and need clarification.

      You need to use code tags, not only for the program code, but also for the the data that the program is supposed to read. In the text of your question, please use code tags to display:
      Mon Feb 20 09:31:25 2017 [INFO] [AGENTEXEC] Error Description.
      code tags also put things into a fixed width font. Your program is unable to parse this line:
      2017-02-20T09:30:53.177000 20[] 0000000000000000 Error Description

      In Perl, it is possible to define a file that is contained within the program code itself! This is called a DATA segment. A simple example:

      #!/usr/bin/perl use strict; use warnings; while (<DATA>) { print; } __DATA__ Some example lines that could be in a file
      The __DATA__ segment is a pre-opened file handle. There are ways to put multiple input files within the code, but a DATA segment for a single file is the most often used.

      It is not clear to me what the desired output is. Please include an example of that in your post.

      Hi, I was referring to the sample data lines you included, which are not in code tags and thus are mangled. This had prevented me from trying to solve your problem ...

      An SSCCE here would, as I said, contain only enough code and data to demonstrate your issue. Using the __DATA__ section in a file allows you to include data and code in the same file but keep them separate. Perfect for an SSCCE. For example:

      use strict; use warnings; use feature 'say'; use DateTime::Format::Flexible; use Test::More tests => 2; my $wanted = '2017-02-20 09:30:53'; for my $string ( <DATA> ) { chomp $string; my $dt = DateTime::Format::Flexible->parse_datetime( $string ); is( $dt->strftime('%F %T'), $wanted, "with >$string<" ); } __DATA__ Mon Feb 20 09:30:53 2017 2017-02-20T09:30:53.177000
      Output:
      1..2 ok 1 - with >Mon Feb 20 09:30:53 2017< ok 2 - with >2017-02-20T09:30:53.177000<
      (edit: updated example with OP's data)

      Hope this helps!


      The way forward always starts with a minimal test.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 07:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found