I need to count number of objects in each group with JQ, but only for N most recent objects.
Sample input, for N=3:
{"modified":"Mon Sep 25 14:20:00 +0000 2018","object_id":1,"group_id":"C"}
{"modified":"Mon Sep 25 14:23:00 +0000 2018","object_id":2,"group_id":"A"}
{"modified":"Mon Sep 25 14:21:00 +0000 2018","object_id":3,"group_id":"B"}
{"modified":"Mon Sep 25 14:22:00 +0000 2018","object_id":4,"group_id":"A"}
Expected output:
{"A",2}
{"B",1}
I'm failing even to select a date-based subset which will preserve the structure of the objects: this is the best I managed to achieve:
[
.modified |= strptime("%a %b %d %H:%M:%S %z %Y") |
.modified |= mktime |
.modified |= strftime("%Y-%m-%d %H:%M:%S")
] |
sort_by(.modified) |
.[] |
{modified, object_id, group_id}
For some reason, results are still unsorted.
I'm also failing to convert such a list to an array to select only N most recent entries.
And after that I will need to count number of objects per group in some way.
Overall, looks like I need an extremely intuitive explanation on how arrays and lists of objects convert to each other, and how to modify some of their fields and, after that, to extract only fields required. The tutorials I've found so far did not help, unfortunately.
Assuming your input file is:
cat file
{"modified":"Mon Sep 25 14:20:00 +0000 2018","object_id":1,"class_id":"C"}
{"modified":"Mon Sep 25 14:23:00 +0000 2018","object_id":2,"class_id":"A"}
{"modified":"Mon Sep 25 14:21:00 +0000 2018","object_id":3,"class_id":"B"}
{"modified":"Mon Sep 25 14:22:00 +0000 2018","object_id":4,"class_id":"A"}
You can try the following:
<file jq -s '
[ .[] |
(.modified |= (strptime("%a %b %d %H:%M:%S +0000 %Y") | mktime))
] |
sort_by(.modified) | # sort using converted time
.[-3:] | # take the last 3
group_by(.class_id) | # group ids together
.[] |
{(.[0].class_id): length}' # create the object using the id name and table length
{
"A": 2
}
{
"B": 1
}
Note that on my system, the option %z of strptime isn't working. So I replaced it with +0000 (which is anyway not used in the time conversion).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With