相关文章推荐
仗义的炒面  ·  jQuery EasyUI 表单 – ...·  5 月前    · 
性感的板凳  ·  wpf listview 点击事件-掘金·  2 年前    · 

使用jq查找并追加json文件中的一个字符串

1 人关注

我有一个Json文件 meta.json ,内容如下,我想在下面的Json文件中进行查找和追加操作,请使用 jq filter .例如,下面的json值 name demo ,所以,给它附加一个字符串,如 -test ,所以最终值将是 demo-test

"version": "2.2.0", "vname": "tf", "data": { "name": "demo", "udn": { "description": "The `main` tf in this template creates a resource`. "

更新后的json文件应包含如下数据

"version": "2.2.0", "vname": "tf", "data": { "name": "demo-test", "udn": { "description": "The `main` tf in this template creates a resource`. "

JQ过滤器是否支持这个? 我们如何才能做到这一点?

1 个评论
So you want to append -test to the name inside data ? .data.name += "-test"
json
linux
shell
sed
jq
dubru
dubru
发布于 2022-05-10
1 个回答
HatLess
HatLess
发布于 2022-05-10
已采纳
0 人赞同

Using jq

$ jq '.data.name |= . + "-test"' meta.json
  "version": "2.2.0",
  "vname": "tf",
  "data": {
    "name": "demo-test",
    "udn": {
      "description": "The `main` tf in this template creates a resource`. "

Using sed

$ sed '/\<name\>/s/[[:punct:]]\+$/-test&/' meta.json
  "version": "2.2.0",
  "vname": "tf",
  "data": {
    "name": "demo-test",
    "udn": {