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

Re^4: Epoch based parser

by spikeinc (Acolyte)
on Aug 14, 2013 at 07:37 UTC ( #1049395=note: print w/replies, xml ) Need Help??

in reply to Re^3: Epoch based parser
in thread Epoch based parser

Hi Ken,

Thanks for a quick response.

Note: you haven't supplied data that matches your original 12:15 or subsequent any:20 I've made an additional change in order to get some output.

indeed I have two scripts, one that does time based as you had given me, and the other one that takes in file as an input and then parses json. This script (in the above post) takes input (as json output from a webpage logged to the file) and parses it.

I want to attach the file but I am not sure how I can do that. I tried it as above but it gives me the foll. error:

use strict; use warnings; use Time::Local; use JSON; use File::Read; my $jsonc = read_file ('H:\Work\perl\latest\Scripts\logs\json.txt'); { local $/ = "}\n"; while ($jsonc) { s/\n[+]?//gm; my $data = decode_json $_; for (@{$data->{aaData}}) { #my print/parse function } } }
What does having while(<DATA>) do? Does it read till end of the _DATA_ ? if so is it correct that in my above case I do a while($jsonc)? I understood the substitution part, thanks a lot. However, in my case when I parse my file I get the below error:
c:\perl>perl Use of uninitialized value $_ in substitution (s///) at line 14 +. malformed JSON string, neither array, object, number, string or atom, +at character offset 0 (before "(end of string )") at line 15.
Content of json.txt looks pretty much same as what you have mentioned above in the _DATA_ section.And I can clearly see that the end of first json block has a "]]}\n". So its supposed to work as you mentioned.

Replies are listed 'Best First'.
Re^5: Epoch based parser
by kcott (Canon) on Aug 14, 2013 at 09:26 UTC

    When I first read your latest post, the code looked like this:

    my $jsonc = read_file ('H:\Work\perl\latest\Scripts\logs\json.txt'); local $/ = "}"; while ($jsonc) { s/\n[+]?//gm; my $data = decode_json $jsonc; for (@{$data->{aaData}}) { #my print function }

    I wrote some more example code, dug up a few more references to help you out and started to respond. Upon doing so, I find you've changed that code to this:

    my $jsonc = read_file ('H:\Work\perl\latest\Scripts\logs\json.txt'); { local $/ = "}\n"; while ($jsonc) { s/\n[+]?//gm; my $data = decode_json $_; for (@{$data->{aaData}}) { #my print/parse function } } }

    If you make changes then clearly indicate what you've changed! See "How do I change/delete my post?".

    Here's the new example code I wrote:

    #!/usr/bin/env perl -l use strict; use warnings; use JSON; use Time::Piece; my $json_file = 'json.txt'; my $wanted_minute = 40; open my $json_fh, '<', $json_file or die "Can't read '$json_file': $!" +; { local $/ = "}\n"; while (<$json_fh>) { s/\n[+]?//gm; my $data = decode_json $_; for (@{$data->{aaData}}) { print "@$_" if is_wanted_time(@$_[0,1]); } } } close $json_fh; sub is_wanted_time { for (@_) { my $t = gmtime $_; return 1 if $t->min == $wanted_minute; } return 0; }

    With this input:

    $ cat json.txt {"DisplayRecords":"12","Records":"12","sColumns":"startTime,endTime, remoteNode,srcIP,srcPort,destIP,destPort,egress,ingress","aaData":[["1 +375976271" ,"1375976430","LAN","D0:05:FE","",1093,"",1330," +1034,348"]]} {"DisplayRecords":"12","Records":"12","sColumns":"startTime,endTime, remoteNode,srcIP,srcPort,destIP,destPort,egress,ingress","aaData":[["1 +375976271" ,"1375976430","LAN","D0:05:FE","",1093,"",1330," +1034,348"]]}

    Here's the output (it's the same as from the last example, i.e.

    $ 1375976271 1375976430 LAN D0:05:FE 1093 1330 1 +034,348 1375976271 1375976430 LAN D0:05:FE 1093 1330 1 +034,348

    Here's some more references:

    -- Ken

      G'Day Ken. I am extremely sorry for doing the edits without notifying. I was trying out couple of things and wanted to post the latest.

      However, your logic really helped and I thank you for that. I learnt a few things and also implemented some more regexp related filtering and it works great!!!

      I appreciate your help and thankful to you in helping me learn perl so far :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1049395]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2017-01-20 20:14 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (177 votes). Check out past polls.