Skip to content

Instantly share code, notes, and snippets.

@tst2005
Last active May 24, 2018 16:53
Show Gist options
  • Save tst2005/1ff0040e33621e90734b9c1fc735a23f to your computer and use it in GitHub Desktop.
Save tst2005/1ff0040e33621e90734b9c1fc735a23f to your computer and use it in GitHub Desktop.
getrawdata() {
printf '[
{ "id": "bar", "info": "A babab" },
{ "id": "buz", "info": "B ububu" },
{ "id": "foo", "info": "A fofof" }
]';
}
rawdata_to_db() {
jq '{db: .}';
}
keepresult() {
jq '(.result|map({key:., value:true})|from_entries) as $want | .db |= map(select($want[.id]))|del(.result)'
}
search() {
local arg="$1"
jq '
def grep($arg): test($arg);
def myfilter: '"$arg"';
.+{ result: [.db[]|select( myfilter ).id] }
'
}
getdb() {
getrawdata | rawdata_to_db
}
dbquery() {
search "$1" | keepresult
}
sample() {
getdb |
dbquery '.info|grep("^A")' |
dbquery '(.info|grep("^Z")|not)' |
dbquery '.id!="zz"'
}
sample
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment