1

I am a beginner for any sort of regex. I need your help/pointers in resolving an issue. I have a JSON file which looks like this below.

JSON format

{"record-type":"int-stats","time":1389309548046925,"host-id":"a.b.c.d","port":"ab-0/0/44","latency":108992}
{"record-type":"int-stats","time":1389309548046925,"host-id":"x.x.x.x","port":"ab-0/0/45","latency":36940}
{"record-type":"int-stats","time":1389309548046925,"host-id":"x.x.x.x","port":"ab-0/0/46","latency":11315}
{"record-type":"int-stats","time":1389309548046925,"host-id":"x.x.x.x","port":"ab-0/0/47","latency":102668}
{"record-type":"int-stats","time":1389309548046925,"host-id":"x.x.x.x","port":"ab-0/0/9","latency":347776}

{"record-type":"int-stats","time":1389309548041555,"host-id":"a.b.c.d","port":"ab-0/0/44","latency":108992}
{"record-type":"int-stats","time":1389309548041554,"host-id":"x.x.x.x","port":"ab-0/0/45","latency":36940}
{"record-type":"int-stats","time":1389309548046151,"host-id":"x.x.x.x","port":"ab-0/0/46","latency":11315}
{"record-type":"int-stats","time":1389309548041667,"host-id":"x.x.x.x","port":"ab-0/0/47","latency":102668}
{"record-type":"int-stats","time":1389309548042626,"host-id":"x.x.x.x","port":"ab-0/0/9","latency":347776}

{"record-type":"int-stats","time":1389309548035666,"host-id":"a.b.c.d","port":"ab-0/0/44","latency":108992}
{"record-type":"int-stats","time":1389309548035635,"host-id":"x.x.x.x","port":"ab-0/0/45","latency":36940}
{"record-type":"int-stats","time":1389309548042255,"host-id":"x.x.x.x","port":"ab-0/0/46","latency":11315}
{"record-type":"int-stats","time":1389309548041715,"host-id":"x.x.x.x","port":"ab-0/0/47","latency":102668}
{"record-type":"int-stats","time":1389309548046161,"host-id":"x.x.x.x","port":"ab-0/0/9","latency":347776}

{"record-type":"int-stats","time":1389309548023422,"host-id":"a.b.c.d","port":"ab-0/0/44","latency":108992}
{"record-type":"int-stats","time":1389309548041617,"host-id":"x.x.x.x","port":"ab-0/0/45","latency":36940}
{"record-type":"int-stats","time":1389309548046676,"host-id":"x.x.x.x","port":"ab-0/0/46","latency":11315}
{"record-type":"int-stats","time":1389309548045675,"host-id":"x.x.x.x","port":"ab-0/0/47","latency":102668}
{"record-type":"int-stats","time":1389309548046172,"host-id":"x.x.x.x","port":"ab-0/0/9","latency":347776}

{"record-type":"int-stats","time":1389309548034534,"host-id":"a.b.c.d","port":"ab-0/0/44","latency":108992}
{"record-type":"int-stats","time":1389309548012345,"host-id":"x.x.x.x","port":"ab-0/0/45","latency":36940}
{"record-type":"int-stats","time":1389309548025232,"host-id":"x.x.x.x","port":"ab-0/0/46","latency":11315}
{"record-type":"int-stats","time":1389309548023423,"host-id":"x.x.x.x","port":"ab-0/0/47","latency":102668}
{"record-type":"int-stats","time":1389309548252352,"host-id":"x.x.x.x","port":"ab-0/0/9","latency":347776}

I need to extract "port":"ab-0/0/44" and associated "time" with that port. I am trying to calculate the time difference for any two such occurrences, i.e 1st occurrence-> "time":1389309548046925 "port":"ab-0/0/44" 2nd occurrence -> "time":1389309548041555 "port":"ab-0/0/44". The calculated time difference must be stored in a variable. I tried with a regular expression like this /\"time\":\\d+\.*\"port\":\".b-0\/0\/44\"/. Any help is appreciated. Thanks in advance!

TLP
  • 66,756
  • 10
  • 92
  • 149
spamulap12
  • 97
  • 1
  • 9

1 Answers1

2

Use the JSON module. It's rather simple.

use strict;
use warnings;
use JSON;

while (<>) {
    /\S/ or next;
    my $data = decode_json($_);
    print "port -> $data->{port}\n";
    print "time -> $data->{time}\n";
}

With your data, I get output like this:

port -> ab-0/0/44
time -> 1389309548046925
port -> ab-0/0/45
time -> 1389309548046925
... etc

I'm not sure how you want to calculate your time, but I assume that doing arithmetic is something you can figure out best on your own.

TLP
  • 66,756
  • 10
  • 92
  • 149
  • Hi..thanks much for your pointer. I am also very new to perl. Sorry, if I am not clear on my question. But, I need only the ports with value ab-0/0/44 and their respective times. I have this JSON data repeated with same port but with different time stamps few thousands of times and I am looking for exactly two such occurrences. Not more than that. Thanks in advance! – spamulap12 Feb 19 '14 at 22:49
  • I have shown you how to parse the JSON data. Do you mean to say that you do not understand Perl well enough to use the parsed data to do what you want? – TLP Feb 19 '14 at 23:05
  • Yeah. I do not understand Perl well as of now. But I will very soon :) How do I store into an array? I appreciate your time and effort – spamulap12 Feb 19 '14 at 23:23
  • Perhaps you should take a look at [perldoc perlfunc](http://perldoc.perl.org/perlfunc.html). There you will find functions related to arrays. You might also like [perldoc perlop](http://perldoc.perl.org/perlop.html) where you might find operators to compare strings (`eq` etc). – TLP Feb 19 '14 at 23:34
  • Thanks a lot! I figured out. All your suggestions are helpful. – spamulap12 Feb 19 '14 at 23:38