Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Epoch based parser

by kcott (Abbot)
on Aug 09, 2013 at 11:00 UTC ( #1048742=note: print w/ replies, xml ) Need Help??


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


Comment on Re: Epoch based parser
Select or Download Code
Reaped: Re^2: Epoch based parser
by NodeReaper (Curate) on Aug 09, 2013 at 18:36 UTC
Re^2: Epoch based parser
by spikeinc (Acolyte) on Aug 09, 2013 at 18:57 UTC
    Thankyou Ken for saving my day!!

    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

      "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

        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!
        Hi Ken,

        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

        #!/usr/bin/env perl -l use strict; use warnings; use Time::Local; use JSON; use Time::Piece; my $poll_hour = 1; my $poll_minute = 20; my $data = decode_json join '' => map { chomp; $_ } (<DATA>); use Text::Table; my $tb = Text::Table->new( "Date", "", "Start Time", "End Time", "Time Taken" , "Egress:I +ngress" , "AADATA" ); for (@{$data->{aaData}}) { my $difftime=(@$_[1] - @$_[0]); if (is_poll_time(@$_[0,1])) { # print "@$_"; @$_[0] = scalar(localtime(@$_[0])); @$_[1] = scalar(localtime(@$_[1])); my $day = substr(@$_[0],0,10); my $stime = substr(@$_[0],11,8); my $year = substr(@$_[0],20,4); my $etime = substr(@$_[1],11,8); #my @dateend = split(' ',@$_[1])); $tb->load( [ $day, $year, $stime , $etime ,convert_time($difftime) , @$_[ +8] , @$_[9] ], ); } } print $tb; sub is_poll_time { for ($poll_hour=1; $poll_hour<=24; $poll_hour++){ for (@_) { my $t = gmtime $_; return 1 if $t->hour == $poll_hour && $t->min == $poll_minute; } } return 0; } sub convert_time { my $time = shift; my $days = int($time / 86400); $time -= ($days * 86400); my $hours = int($time / 3600); $time -= ($hours * 3600); my $minutes = int($time / 60); my $seconds = $time % 60; $days = $days < 1 ? '' : $days .'d '; $hours = $hours < 1 ? '' : $hours .'h '; $minutes = $minutes < 1 ? '' : $minutes . ' min '; $time = $days . $hours . $minutes . $seconds . ' sec'; return $time; } __DATA__ {"iTotalDisplayRecords":"90","iTotalRecords":"90","sColumns":"startTim +e,endTime,remoteNode,srcIP,srcPort,destIP,destPort,egress,ingress","a +aData":[["1375856507","137 5858307","WAN","FF:80:D1","192.168.167.37",3649,"10.219.72.3",80,"0,20 +","149,186"],["1375856506","1375858307","WAN","FF:C3:77","192.168.167 +.28",3910,"10.219.72.3",80 ,"65536,65641","149,188"],["1375856505","1375858305","WAN","FF:C9:7D", +"192.168.167.9",1944,"10.219.72.3",80,"1935584,273470","150,264"],["1 +375856501","1375858301","W AN","FF:C2:DB","192.168.167.30",4606,"10.219.72.3",80,"1608068,1118911 +","132,189"],["1375856498","1375858298","WAN","FF:C5:4E","192.168.167 +.38",2611,"10.219.72.3",80 ,"920588,455833","150,193"],["1375856495","1375858295","WAN","FF:C7:90 +","192.168.167.16",2036,"10.219.72.3",80,"1755036,470171","147,185"], +["1375856494","1375858294" ,"WAN","FF:C1:FD","192.168.167.34",2118,"10.219.72.3",80,"633624,58520 +7","177,224"],["1375856491","1375858291","WAN","FF:C4:EF","192.168.16 +7.23",3875,"10.219.72.3",8 0,"2316316,1906209","132,191"],["1375856471","1375858271","WAN","FF:C6 +:67","192.168.167.5",4794,"10.219.72.3",80,"9765028,4016161","147,103 +4"],["1375856470","1375858 271","WAN","FF:CD:09","192.168.167.11",1707,"10.219.72.3",80,"2860552, +533884","168,199"],["1375856465","1375858265","WAN","FF:C7:E1","192.1 +68.167.15",2142,"10.219.72 .3",80,"6639576,1934965","147,185"],["1375856459","1375858260","WAN"," +FF:C2:99","192.168.167.31",4478,"10.219.72.3",80,"16729692,3831326"," +146,260"],["1375856445","1 375858246","WAN","FF:C6:CC","192.168.167.22",4674,"10.219.72.3",80,"42 +30804,3165895","147,691"],["1375856437","1375858237","WAN","FF:C9:F9" +,"192.168.167.4",4956,"10. 219.72.3",80,"9396960,1990474","147,185"],["1375856433","1375858234"," +WAN","FF:C9:A7","192.168.167.17",3899,"10.219.72.3",80,"9708088,78299 +78","147,9798"],["13758564 32","1375858232","WAN","FF:C8:23","192.168.167.2",3204,"10.219.72.3",8 +0,"12722744,8198182","147,308"],["1375856432","1375858232","WAN","FF: +C3:EA","192.168.167.27",44 49,"10.219.72.3",80,"8621880,8641627","146,240"],["1375856412","137585 +8213","WAN","FF:C7:E8","192.168.167.13",1319,"10.219.72.3",80,"119101 +96,11969965","147,1460"],[ "1375856370","1375858170","WAN","FF:CA:4E","192.168.167.14",3123,"10.2 +19.72.3",80,"12963152,5276668","147,665"],["1375856504","1375857704", +"WAN","FF:F8:7C","192.168. 167.57",65011,"10.219.72.3",53517,"1642500,308139","0,16"],["137585649 +5","1375857695","WAN","FF:F8:0D","192.168.167.56",63669,"10.219.72.3" +,80,"4042956,1150040","177 ,207"],["1375856485","1375857685","WAN","FF:F8:7C","192.168.167.57",65 +005,"10.219.72.3",80,"1546748,821480","177,208"],["1375856495","13758 +57510","WAN","FF:C8:A2","1 92.168.167.19",2598,"10.219.72.3",80,"4138988,1346531","168,319"],["13 +75856488","1375857499","WAN","FF:C9:25","192.168.167.10",1159,"10.219 +.72.3",80,"4595712,3295110 ","147,730"],["1375856497","1375857481","WAN","FF:C7:65","192.168.167. +20",4464,"10.219.72.3",80,"4903764,1402164","168,267"],["1375856497", +"1375857476","WAN","FF:C5: B6","192.168.167.21",1802,"10.219.72.3",80,"4182311,1138853","150,741" +],["1375856502","1375857475","WAN","FF:F8:48","192.168.167.59",51847, +"10.219.72.3",53511,"26126 00,394743","0,16"],["1375856497","1375857474","WAN","FF:83:4D","192.16 +8.167.26",4174,"10.219.72.3",80,"2752512,763324","167,215"],["1375856 +502","1375857474","WAN","F F:F8:48","192.168.167.59",51844,"10.219.72.3",80,"566384,201703","146, +185"],["1375856474","1375857472","WAN","FF:C8:81","192.168.167.18",10 +78,"10.219.72.3",80,"33669 12,696540","168,294"],["1375856504","1375857453","WAN","FF:C3:54","192 +.168.167.29",1846,"10.219.72.3",80,"1241616,696810","177,209"],["1375 +856503","1375856508","WAN" ,"FF:CD:4A","192.168.167.7",1400,"10.219.72.3",80,"2389792,167571","13 +6,203"],["1375856490","1375856507","WAN","FF:CE:56","192.168.167.3",2 +011,"10.219.72.3",80,"4182 311,850324","150,183"],["1375856498","1375856506","WAN","FF:CA:6B","19 +2.168.167.12",1981,"10.219.72.3",80,"2699046,1124066","133,756"],["13 +75856501","1375856506","WA N","FF:C6:E3","192.168.167.6",3140,"10.219.72.3",80,"2257178,494035"," +133,174"],["1375856470","1375856505","WAN","FF:C3:77","192.168.167.28 +",3879,"10.219.72.3",80,"4 161362,4123838","177,242"],["1375856503","1375856505","WAN","FF:F8:0D" +,"192.168.167.56",63674,"10.219.72.3",53514,"4563776,798503","0,20"], +["1375856491","1375856503" ,"WAN","FF:F8:7C","192.168.167.57",65008,"10.219.72.3",53475,"4563776, +642254","0,20"],["1375856502","1375856502","WAN","FF:F8:48","192.168. +167.59",51842,"10.219.72.3 ",80,"1429,824","132,175"],["1375856497","1375856502","WAN","FF:F8:48" +,"192.168.167.59",51840,"10.219.72.3",53496,"5338624,714602","0,20"], +["1375856480","1375856501" ,"WAN","FF:C3:54","192.168.167.29",1828,"10.219.72.3",80,"6128421,3193 +443","167,198"],["1375856490","1375856501","WAN","FF:F8:48","192.168. +167.59",51831,"10.219.72.3 ",80,"2389792,212440","135,175"],["1375856488","1375856501","WAN","FF: +C9:7D","192.168.167.9",1941,"10.219.72.3",80,"4161362,674756","178,24 +0"],["1375856477","1375856 500","WAN","FF:CD:4A","192.168.167.7",1390,"10.219.72.3",80,"2699046,4 +45565","133,204"],["1375856481","1375856500","WAN","FF:C6:E3","192.16 +8.167.6",3136,"10.219.72.3 ",80,"4182311,1278644","150,185"],["1375856487","1375856499","WAN","FF +:C2:DB","192.168.167.30",4604,"10.219.72.3",80,"2834057,1749320","132 +,197"],["1375856466","1375 856498","WAN","FF:C5:4E","192.168.167.38",2577,"10.219.72.3",80,"41613 +62,1507534","178,232"],["1375856485","1375856497","WAN","FF:CA:6B","1 +92.168.167.12",1977,"10.21 9.72.3",80,"2834057,1171220","133,304"],["1375856495","1375856496","WA +N","FF:F8:48","192.168.167.59",51837,"10.219.72.3",53490,"2384896,222 +035","0,20"],["1375856408" ,"1375856496","WAN","FF:83:4D","192.168.167.26",4108,"10.219.72.3",80, +"17796913,7366167","146,331"],["1375856494","1375856496","WAN","FF:F8 +:0D","192.168.167.56",6366 7,"10.219.72.3",53486,"5338624,1082762","0,20"],["1375856484","1375856 +495","WAN","FF:C5:B6","192.168.167.21",1799,"10.219.72.3",80,"4161362 +,1316715","178,554"],["137 5856483","1375856494","WAN","FF:F8:0D","192.168.167.56",63656,"10.219. +72.3",80,"6128421,2735602","167,196"],["1375856493","1375856494","WAN +","FF:C7:90","192.168.167. 16",2034,"10.219.72.3",80,"1429,824","133,176"],["1375856434","1375856 +494","WAN","FF:C8:A2","192.168.167.19",2593,"10.219.72.3",80,"1779691 +3,5349326","147,912"],["13 75856440","1375856494","WAN","FF:C7:65","192.168.167.20",4461,"10.219. +72.3",80,"17796913,4219966","147,993"],["1375856489","1375856493","WA +N","FF:F8:0D","192.168.167 .56",63664,"10.219.72.3",53464,"2384896,226987","0,20"],["1375856472", +"1375856492","WAN","FF:C7:90","192.168.167.16",2031,"10.219.72.3",80, +"2389792,204182","136,176" ],["1375856379","1375856492","WAN","FF:C1:FD","192.168.167.34",2096,"1 +0.219.72.3",80,"6128421,4529607","167,197"],["1375856491","1375856491 +","WAN","FF:F8:48","192.16 8.167.59",51834,"10.219.72.3",53476,"0,4","0,4"],["1375856482","137585 +6491","WAN","FF:F8:48","192.168.167.59",51829,"10.219.72.3",53437,"22 +56896,427276","0,20"],["13 75856483","1375856491","WAN","FF:F8:7C","192.168.167.57",65003,"10.219 +.72.3",53441,"5338624,792304","0,20"],["1375856481","1375856490","WAN +","FF:F8:48","192.168.167. 59",51823,"10.219.72.3",80,"2699046,1067833","132,174"],["1375856484", +"1375856490","WAN","FF:C4:EF","192.168.167.23",3869,"10.219.72.3",80, +"2257178,1508892","132,198 "],["1375856489","1375856489","WAN","FF:F8:0D","192.168.167.56",63661, +"10.219.72.3",53462,"0,4","0,4"],["1375856487","1375856487","WAN","FF +:C9:25","192.168.167.10",1 157,"10.219.72.3",80,"1429,822","133,173"],["1375856467","1375856487", +"WAN","FF:CE:56","192.168.167.3",2006,"10.219.72.3",80,"4161362,96468 +8","178,209"],["1375856444 ","1375856486","WAN","FF:C9:7D","192.168.167.9",1938,"10.219.72.3",80, +"6128421,864904","168,340"],["1375856484","1375856485","WAN","FF:F8:0 +D","192.168.167.56",63658, "10.219.72.3",53443,"2256896,1039336","0,20"],["1375856468","137585648 +5","WAN","FF:C2:DB","192.168.167.30",4574,"10.219.72.3",80,"2257178,3 +18147","132,173"],["137585 6464","1375856484","WAN","FF:F8:7C","192.168.167.57",64968,"10.219.72. +3",80,"6128421,2522269","167,197"],["1375856463","1375856484","WAN"," +FF:CA:6B","192.168.167.12" ,1973,"10.219.72.3",80,"2257178,578070","133,1147"],["1375856471","137 +5856483","WAN","FF:C9:25","192.168.167.10",1154,"10.219.72.3",80,"238 +9792,287545","136,214"],[" 1375856364","1375856483","WAN","FF:F8:0D","192.168.167.56",63540,"10.2 +19.72.3",80,"17796913,10834116","146,183"],["1375856482","1375856483" +,"WAN","FF:F8:0D","192.168 .167.56",63653,"10.219.72.3",53436,"1032192,202992","0,20"],["13758564 +50","1375856483","WAN","FF:C4:EF","192.168.167.23",3859,"10.219.72.3" +,80,"4182311,2673002","149 ,249"],["1375856453","1375856483","WAN","FF:C5:B6","192.168.167.21",17 +96,"10.219.72.3",80,"6128421,1720678","168,701"],["1375856475","13758 +56482","WAN","FF:F8:7C","1 92.168.167.57",65000,"10.219.72.3",53398,"2384896,225894","0,20"],["13 +75856450","1375856482","WAN","FF:80:D1","192.168.167.37",3643,"10.219 +.72.3",80,"4161362,871817" ,"177,261"],["1375856481","1375856481","WAN","FF:F8:48","192.168.167.5 +9",51826,"10.219.72.3",53431,"1032192,166348","0,20"],["1375856480"," +1375856481","WAN","FF:F8:0 D","192.168.167.56",63650,"10.219.72.3",53430,"1628016,598969","0,20"] +,["1375856450","1375856481","WAN","FF:C6:E3","192.168.167.6",3132,"10 +.219.72.3",80,"4161362,102 6987","178,209"],["1375856480","1375856481","WAN","FF:F8:48","192.168. +167.59",51821,"10.219.72.3",53428,"1628016,300089","0,20"],["13758564 +51","1375856480","WAN","FF :F8:48","192.168.167.59",51785,"10.219.72.3",80,"2834057,889744","132, +175"],["1375856479","1375856480","WAN","FF:F8:0D","192.168.167.56",63 +647,"10.219.72.3",53427,"1 162752,96819","0,20"],["1375856479","1375856479","WAN","FF:F8:48","192 +.168.167.59",51818,"10.219.72.3",53426,"1162752,98143","0,20"],["1375 +856475","1375856479","WAN" ,"FF:F8:0D","192.168.167.56",63644,"10.219.72.3",53393,"4252672,107755 +7","0,20"],["1375856477","1375856479","WAN","FF:F8:48","192.168.167.5 +9",51815,"10.219.72.3",534 20,"4252672,618283","0,20"],["1375856401","1375856477","WAN","FF:C3:54 +","192.168.167.29",1786,"10.219.72.3",80,"17796913,3514777","146,185" +],["1375856475","137585647 7","WAN","FF:F8:48","192.168.167.59",51812,"10.219.72.3",53401,"456377 +6,834693","0,20"]]}

        And the output:

        Date Start Time End Time Time Taken Egress:Ingress AAD +ATA Tue Aug 6 2013 23:20:59 23:51:00 30 min 1 sec 16729692,3831326 146 +,260 Tue Aug 6 2013 23:20:45 23:50:46 30 min 1 sec 4230804,3165895 147 +,691 Tue Aug 6 2013 23:20:37 23:50:37 30 min 0 sec 9396960,1990474 147 +,185 Tue Aug 6 2013 23:20:33 23:50:34 30 min 1 sec 9708088,7829978 147 +,9798 Tue Aug 6 2013 23:20:32 23:50:32 30 min 0 sec 12722744,8198182 147 +,308 Tue Aug 6 2013 23:20:32 23:50:32 30 min 0 sec 8621880,8641627 146 +,240 Tue Aug 6 2013 23:20:12 23:50:13 30 min 1 sec 11910196,11969965 147 +,1460 Tue Aug 6 2013 23:20:08 23:21:36 1 min 28 sec 17796913,7366167 146 +,331 Tue Aug 6 2013 23:20:34 23:21:34 1 min 0 sec 17796913,5349326 147 +,912 Tue Aug 6 2013 23:20:40 23:21:34 54 sec 17796913,4219966 147 +,993 Tue Aug 6 2013 23:20:44 23:21:26 42 sec 6128421,864904 168 +,340 Tue Aug 6 2013 23:20:50 23:21:23 33 sec 4182311,2673002 149 +,249 Tue Aug 6 2013 23:20:53 23:21:23 30 sec 6128421,1720678 168 +,701 Tue Aug 6 2013 23:20:50 23:21:22 32 sec 4161362,871817 177 +,261 Tue Aug 6 2013 23:20:50 23:21:21 31 sec 4161362,1026987 178 +,209 Tue Aug 6 2013 23:20:51 23:21:20 29 sec 2834057,889744 132 +,175 Tue Aug 6 2013 23:20:01 23:21:17 1 min 16 sec 17796913,3514777 146 +,185
Re^2: Epoch based parser
by spikeinc (Acolyte) on Aug 14, 2013 at 02:56 UTC
    Hi Ken,

    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:

    #!/usr/bin/env perl -l use strict; use warnings; use Time::Local; use JSON; use Time::Piece; use Math::Round; use File::Read; use Text::Table; my $DestIP="192.168.127.111"; my $jsonc = read_file ('C:\Documents and Settings\SonicWALL User\My Do +cuments\Scripts\logs\json.txt'); my $data = decode_json join '' => map { chomp; $_ } ($jsonc); #my $data = decode_json $jsonc; my $tb = Text::Table->new( "Date", "Start Time", "End Time", "Time Taken" , "Egress: Conv +eyed", "Egress : Sent" , "Ingress: Conveyed" , "Ingress: Sent"); for (@{$data->{aaData}}) { my $difftime=(@$_[1] - @$_[0]); my $destip=@$_[6]; my @egress=split(',', @$_[8]); my @ingress=split(',', @$_[9]); my $econveyed=$egress[0]/1000000; my $esent=$egress[1]/1000000; my $iconveyed=$ingress[0]/1000; my $isent=$ingress[1]/1000; my $startTime=scalar(localtime(@$_[0])); my $endTime=scalar(localtime(@$_[1])); #Filter only for required traffic and eliminate any traffic less than + 1MB if ( $destip eq $DestIP && $egress[0]>1000000) { #print "@$_"; my $date = $startTime->mdy; my $stime1 = $startTime->hms; my $etime1 =$endTime->hms; $tb->load( [ $date, $stime1 , $etime1 ,convert_time($difftime) , nearest(.01,$e +conveyed) , nearest(.01,$esent), $iconveyed , $isent ], ); } } print $tb;
    The file input would be something like:
    {"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"]]} {"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"]]} .... and so on

    I couldnt figure this part out. Thanks for your help so far.

      "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.]

      #!/usr/bin/env perl -l use strict; use warnings; use JSON; use Time::Piece; my $wanted_minute = 40; { local $/ = "}\n"; while (<DATA>) { s/\n[+]?//gm; my $data = decode_json $_; for (@{$data->{aaData}}) { print "@$_" if is_wanted_time(@$_[0,1]); } } } sub is_wanted_time { for (@_) { my $t = gmtime $_; return 1 if $t->min == $wanted_minute; } 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"]]} {"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"]]}

      Output:

      $ pm_epoch_from_json_2.pl 1375976271 1375976430 LAN D0:05:FE 172.20.30.2 1093 172.20.28.2 1330 1 +034,348 1375976271 1375976430 LAN D0:05:FE 172.20.30.2 1093 172.20.28.2 1330 1 +034,348
      "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

        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 json.pl Use of uninitialized value $_ in substitution (s///) at get.pl line 14 +. malformed JSON string, neither array, object, number, string or atom, +at character offset 0 (before "(end of string )") at get.pl 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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2014-07-30 21:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (241 votes), past polls