在bash脚本中将sed与jq一起使用

如何在sq中使用sed将键名中的_替换为符号a

{“ product_name”:“ kl”}

应该成为

{“ productaname”:“ kl”}

在bash脚本中

评论
iodit
iodit

No need for sed; it's easy to do in just jq:

$ jq '{ productaname: .product_name }' <<<'{ "product_name":"kl" }'
{"productaname":"kl"}

如果要在对象的所有键中将下划线替换为a:

$ jq 'with_entries(.key |= gsub("_"; "a"))' <<<'{ "product_name":"kl", "foo_bar":12 }'
{"productaname":"kl","fooabar":12}

From the documentation for with_entries:

to_entries, from_entries, with_entries

These functions convert between an object and an array of key-value pairs. If to_entries is passed an object, then for each k: v entry in the input, the output array includes {"key": k, "value": v}.

from_entries does the opposite conversion, and with_entries(foo) is a shorthand for to_entries | map(foo) | from_entries, useful for doing some operation to all keys and values of an object. from_entries accepts key, Key, name, Name, value and Value as keys.

点赞
评论