简体   繁体   English

我如何使用jq库将字符替换为bash中的空白

[英]How can i replace a character with blank space in bash using jq library

I am using jq command to replace "value" for corresponding "key" in a JSON file. 我正在使用jq命令将JSON文件中对应的“键”的“值”替换为。 Here in the following code snippet I have to replace v 's value corresponding to key k in the JSON object node . 在下面的代码片段中,我必须替换与JSON对象node中的键k对应的v值。

  jq --arg flag "$node" \
                   --arg temp  $v \
             '(.[$flag]|.['$k'])|=$temp' properties.json > tempprop.json

The problem is if there is already a value is present then I need to pass an empty string as value. 问题是如果已经存在一个值,那么我需要传递一个空字符串作为值。 But jq parser is throwing exception saying 但是jq解析器抛出异常说

jq: error: properties/0 is not defined at <top-level>, line 1:
properties.json

So I don't know any other way to deal with this, appreciate any kind of help. 因此,我不知道有什么其他方法可以解决此问题,感谢您提供任何帮助。

Thanks. 谢谢。

If v is an empty string, you need to quote its expansion. 如果v是空字符串,则需要引用其扩展名。 You should pass the value of $k via a variable as well, rather than attempting to interpolate it. 您还应该通过变量传递$k的值,而不是尝试对其进行插值。

jq --arg flag "$node" \
   --arg temp  "$v" \
   --arg k1 "$k" \
   '(.[$flag]|.[$k1])|=$temp' properties.json > tempprop.json

As is, the unquoted expansion of empty $v "disappears", and jq sees the filter as the second argument to that --arg option, leaving properties.json to be interpreted as the filter. --arg ,空$v的无引号扩展“消失”,并且jq将过滤器视为该--arg选项的第二个参数,而将properties.json解释为过滤器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM