in reply to Epoch based parser
G'day spikeinc,
Here's a technique for doing what you want.
I couldn't find any instances of 12:15 in the data you provided; I've used 15:13 as it did appear a few times. You may want localtime instead of the gmtime I've used here (you didn't specify your requirements in this regard). The JSON decoding was straightforward. Check the Time::Piece documentation to see how I've used it and for ways to format the output (if that's what you want).
#!/usr/bin/env perl -l use strict; use warnings; use JSON; use Time::Piece; my $wanted_hour = 15; my $wanted_minute = 13; my $data = decode_json join '' => map { chomp; $_ } (<DATA>); for (@{$data->{aaData}}) { print "@$_" if is_wanted_time(@$_[0,1]); } sub is_wanted_time { for (@_) { my $t = gmtime $_; return 1 if $t->hour == $wanted_hour && $t->min == $wanted_min +ute; } return 0; } __DATA__ {"DisplayRecords":"12","Records":"12","sColumns":"startTime,endTime, remoteNode,srcIP,srcPort,destIP,destPort,egress,ingress","aaData":[["1 +375976271" ,"1375976430","LAN","D0:05:FE","172.20.30.2",1093,"172.20.28.2",1330," +1034,348", "0,0"],["1375976246","1375976404","LAN","D0:05:FE","172.20.30.2",1092, +"172.20.28 .2",1327,"1034,348","0,0"],["1375976191","1375976381","LAN","D0:05:FE" +,"172.20.3 0.2",1091,"172.20.28.2",1324,"1034,348","0,0"],["1375976171","13759763 +14","LAN", "D0:05:FE","172.20.30.2",1090,"172.20.28.2",1321,"1034,348","0,0"],["1 +375976144" ,"1375976302","LAN","D0:05:FE","172.20.30.2",1089,"172.20.28.2",1319," +1034,348", "0,0"],["1375975155","1375976089","LAN","D0:05:FE","172.20.30.2",1085, +"172.20.28 .2",1278,"1034,348","0,0"],["1375975194","1375975352","LAN","D0:05:FE" +,"172.20.3 0.2",1086,"172.20.28.2",1281,"1034,348","0,0"],["1375974693","13759748 +35","LAN", "D0:05:FE","172.20.30.2",1083,"172.20.28.2",1271,"1034,348","0,0"],["1 +375974668" ,"1375974827","LAN","D0:05:FE","172.20.30.2",1081,"172.20.28.2",1268," +1034,348", "0,0"],["1375974691","1375974821","LAN","D0:05:FE","172.20.30.2",1082, +"172.20.28 .2",1270,"0,0","0,0"],["1375881371","1375881561","LAN","D0:05:FE","172 +.20.30.2", 1055,"172.20.28.2",1122,"1034,348","0,0"],["1375881410","1375881540"," +LAN","E0:0 5:AE","172.20.30.2",1056,"172.20.28.2",1125,"0,0","0,0"]]}
Output:
$ pm_epoch_from_json.pl 1375974693 1375974835 LAN D0:05:FE 172.20.30.2 1083 172.20.28.2 1271 1 +034,348 0,0 1375974668 1375974827 LAN D0:05:FE 172.20.30.2 1081 172.20.28.2 1268 1 +034,348 0,0 1375974691 1375974821 LAN D0:05:FE 172.20.30.2 1082 172.20.28.2 1270 0 +,0 0,0
-- Ken
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Epoch based parser
by spikeinc (Acolyte) on Aug 09, 2013 at 18:57 UTC | |
This is pretty much what I wanted. If I would like to print the output with localtime instead of epoch time (in the output) can I use this function : $epochTime=scalar(localtime($Stime));Also instead of a specific time mentioned, if I want to filter restults for every one hour (say first 15:13, then 16:13... 17:13 and so on until the end of data) how could I do that... I am so thankful to u as I am learning perl a lot, i m a very new beginner so making a lot of mistakes hence hoping that your help will make me proceed. thankyou so much again | [reply] [d/l] |
by kcott (Archbishop) on Aug 10, 2013 at 03:01 UTC | |
"If I would like to print the output with localtime instead of epoch time (in the output) can I use this function : ..." Instead of asking if you can use it, just use it. If it does what you want, you've learnt something; if it doesn't do what you want, you've still learnt something; and, if you make a mistake and get a warning or error message, you've learnt even more. It won't melt your keyboard, explode your mouse or cause your screen to burst into flames. :-) "Also instead of a specific time mentioned, if I want to filter restults for every one hour (say first 15:13, then 16:13... 17:13 and so on until the end of data) how could I do that..." That should be really obvious from the code I posted. I strongly recommend that, before reading anything else, you study perlintro -- a brief introduction and overview of Perl. "... I am learning perl a lot, i m a very new beginner so making a lot of mistakes ..." We all make mistakes; even those who've been writing Perl (or, indeed, any language) for decades still mistakes. Mistakes are good: you'll learn a lot from making mistakes. You'll learn nothing from just copying someone else's code (with all the mistakes already fixed). As you're clearly very new to Perl, here's some additional documentation links that may further help with the original code I posted (and with any code you write in the future): If, after writing some code, you encounter a hurdle you can't resolve yourself, post a question here using the guidelines in "How do I post a question effectively?". -- Ken | [reply] [d/l] [select] |
by spikeinc (Acolyte) on Aug 11, 2013 at 10:22 UTC | |
After reading through and doing some homework, I added on to your code to do pretty much what I needed and it works :) . I have finetuned a few things based on my requirements. Here is my code. I might have used a longer way to do things and maybe there are shortcuts to do it. But I would like you to check my code and let me know if what I have done is ok or any enhancements/ improvements can be done. Even if I have done mistakes ,I am quite happy I learnt quite a lot
And the output:
| [reply] [d/l] [select] |
by kcott (Archbishop) on Aug 12, 2013 at 06:35 UTC | |
by spikeinc (Acolyte) on Aug 14, 2013 at 02:25 UTC | |
by spikeinc (Acolyte) on Aug 11, 2013 at 03:12 UTC | |
Thanks Ken! These resource help me. I am doing my homework and trying to make it work. If I have questions I shall contact the forum. Thankyou! | [reply] |
Re^2: Epoch based parser
by spikeinc (Acolyte) on Aug 14, 2013 at 02:56 UTC | |
Finally I made some changes and then also added code to read the json string from a file and parse it. However, my file is going to contain the same kind of json output appended to it continuously over time. When I try to decode_json the entire file, it doesnt allow, as I believe decode_json looks for the braces to see if it finished. How can I parse multiline json from a file? Heres the code I have now: The file input would be something like:
I couldnt figure this part out. Thanks for your help so far. | [reply] [d/l] [select] |
by kcott (Archbishop) on Aug 14, 2013 at 06:48 UTC | |
"When I try to decode_json the entire file, it doesnt allow, as I believe decode_json looks for the braces to see if it finished. How can I parse multiline json from a file?" With the sample input you posted, you can just read records as being delimited with "}\n" by setting the input record separator: $/ (see perlvar: Variables related to filehandles). You can then remove the embedded "\n" and "\n+" strings with s/\n[+]?//gm (see perlre if you're unfamiliar with that). Here's a modification of my original code that does this. [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.]
Output:
"The file input would be something like: ..." I doubt it! That looks like you've just pasted it from the web page including the leading '+'s indicating text wrapping at 70 characters. Assuming that's right, you only need "s/\n//gm" for the regex. -- Ken | [reply] [d/l] [select] |
by spikeinc (Acolyte) on Aug 14, 2013 at 07:37 UTC | |
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: 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: 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. | [reply] [d/l] [select] |
by kcott (Archbishop) on Aug 14, 2013 at 09:26 UTC | |
by spikeinc (Acolyte) on Aug 15, 2013 at 05:41 UTC |