Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i match fields with wildcards using jq?

Tags:

json

jq

I have a JSON object of the following form:

{
  "Task11c-0-20181209-12:59:30-65611" : {
    "attributes" : {
      "configname" : "Task11c",
      "datetime" : "20181209-12:59:30",
      "experiment" : "Task11c",
      "inifile" : "lab1.ini",
      "iterationvars" : "",
      "iterationvarsf" : "",
      "measurement" : "",
      "network" : "Manhattan1_1C",
      "processid" : "65611",
      "repetition" : "0",
      "replication" : "#0",
      "resultdir" : "results",
      "runnumber" : "0",
      "seedset" : "0"
    },
    ......
  },
  ......
  "Task11b-12-20181209-13:03:17-65612" : {
    ....
    .... 
  },
  .......
}

I reported only the first part, but in general I have many other sub-objects which match a string like Task11c-0-20181209-12:59:30-65611. They all have in common the initial word Task. I want to extract the processid from each sub-object. I'm trying to use a wildcard like in bash, but it seems not to be possible.

I also read about the match() function, but it works with strings and not json objects.

Thanks for the support.

like image 764
Alessandro Avatar asked Dec 11 '18 10:12

Alessandro


People also ask

How to use wildcards like ('#name*') in jQuery selectors?

How to use wildcards like ('#name*'), ('#name*'), ('#name%') in jQuery selectors? For getting the id that begins or ends with a particular string in jQuery selectors, you shouldn’t use the wildcards $ ('#name*'), $ ('#name%'). Instead use the characters ^and $.

How do you use wildcard pattern matching?

Wildcard Pattern Matching. Given a text and a wildcard pattern, implement wildcard pattern matching algorithm that finds if wildcard pattern is matched with text. The matching should cover the entire text (not partial text).

How to get the first match in a range using wildcard?

To get the value of the first match in a range using a wildcard, you can use an INDEX and MATCH formula, configured for exact match. In the example shown, the formula in F5 is: =INDEX(B5:D5,MATCH(E5&"*",B5:D5,0))

How to replace a character in a string with a wildcard?

Each occurrence of ‘?’ character in wildcard pattern can be replaced with any other character and each occurrence of ‘*’ with a sequence of characters such that the wildcard pattern becomes identical to the input string after replacement. Let’s consider any character in the pattern.


1 Answers

Filter keys that start with Test and get only the attribute of your choice using the select() expression

jq 'to_entries[] | select(.key|startswith("Task")).value.attributes.processid' json
like image 162
Inian Avatar answered Sep 26 '22 15:09

Inian