Reputation: 3709
I have json file like this one:
{
"abc": "A",
"abc-release": "B",
"bcd":"C",
"cde-release":"D",
"cde":"E"
}
and would like to get resulting file like this:
{
"abc": "B",
"bcd":"C",
"cde":"D"
}
That is:
Need to make this work by using Linux commands like jq
and sed
. Preferably as one-liner with pipes.
I have spent multiple hours trying to implement this and best I could do is this jq command which returns array of keys which i need to delete.
jq "with_entries(select(.key|endswith(\"-release\"))) | keys | map(split(\"-\")[0])"
[
"abc",
"cde"
]
But how to feed this array in jq del
method or some other way to get result what I need?
Second step (renaming elements) is easy. That can be done by jq but also can be done at the end simply by sed:
// simply like this
sed 's/-release//'
// or
sed 's/(.*)-release/$1/'
Upvotes: 1
Views: 172
Reputation: 116670
The following constructive solution using jq has the advantage of being simple and efficient:
. as $in
| reduce (keys_unsorted[] | select(endswith("-release")|not)) as $k ({};
. + {($k) : (($k + "-release") as $kr
| $in
| if has($kr) then .[$kr] else .[$k] end) } )
Upvotes: 3