I have the following JSON:
{
"LaunchTemplates": [
{
"LaunchTemplateName": "bla-99",
"CreateTime": "2022-12-13T13:40:33+00:00"
},
{
"LaunchTemplateName": "abcabc",
"CreateTime": "2022-12-13T09:58:14+00:00"
},
{
"LaunchTemplateName": "bla-34",
"CreateTime": "2022-12-13T13:58:56+00:00"
},
{
"LaunchTemplateName": "bla-222",
"CreateTime": "2022-12-11T13:58:56+00:00"
},
{
"LaunchTemplateName": "bla-233",
"CreateTime": "2022-12-10T13:58:56+00:00"
}
]
}
I want to filter the JSON and print the oldest templates after filtering. I have the following jq
query that prints the template names after filtering:
file.json | jq '.LaunchTemplates[].LaunchTemplateName|select(startswith("bla"))'
Output:
bla-99
bla-34
bla-222
bla-233
Now i want to add more logic to the query, and do something like that: If the number of bla lines is bigger than 3, then print the oldest bla lines (according to the date field). In my case, the output should be:
bla-233
Is that possible with jq
or other shell commands? If so, how?