简体   繁体   English

如何解析jq的bash变量

[英]how to parse bash variable for jq

I have a test.json file which contains below property. 我有一个test.json文件,其中包含以下属性。

"solr.baseuri":"http://10.201.17.8:80"

We are getting expected output for the below script if we parse the exact variable(solr.baseuri) name using jq command. 如果使用jq命令解析确切的变量(solr.baseuri)名称,则将获得以下脚本的预期输出。

Shell script : Shell脚本:

#!/bin/bash

key=`solr.baseuri`

value=`cat test.json |jq '."solr.baseuri"' | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"`

echo "solr value is $value"

Output : 输出:

solr value is 10.201.17.8

not sure how to use the $key with jq command inside the script to display the value. 不知道如何在脚本内使用$ key with jq命令显示值。 trying with below command which is not working 尝试使用下面的命令不起作用

value=`cat test.json |jq '."${key}"' | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"`

I think you just want to pass the key as an --arg : 我认为您只想将密钥作为--arg

jq --arg key "solr.baseuri" '.[$key]' file.json

Testing it out, using the minimal JSON that could be made from the line in your example: 使用示例中的代码行生成的最小JSON对其进行测试:

$ cat file.json 
{ "solr.baseuri":"http://10.201.17.8:80" }
$ jq --arg key "solr.baseuri" '.[$key]' file.json 
"http://10.201.17.8:80"

Rather than piping to grep , you might want to consider using jq built-in functionality to parse the part of the string that you're interested in. 您可能需要考虑使用jq内置功能来解析感兴趣的字符串部分,而不是管道传递给grep

To extract the host to a variable, you could use capture : 要将主机提取为变量,可以使用capture

$ value=$(jq --arg key solr.baseuri -r '.[$key] | capture(".*://(?<host>[^:]+)") | .host' file.json)
$ echo "$value"
10.201.17.8

This assumes that the property always is of the form scheme://host , optionally followed by :port . 假定该属性始终采用scheme://host的形式,并可选地后面跟:port

You can do everything in jq alone. 您可以仅在jq中完成所有操作。 You can probably do it better than the following, even: 您甚至可以比以下方法做得更好:

#!/usr/bin/env bash

key="solr.baseuri"
jq --arg key "$key" '
  .[$key] | split("/") | .[2] | split(":") | .[0]' test.json

This uses the shell variable $key as an argument to jq. 这使用外壳程序变量$key作为jq的参数。 Jq then splits the resultant URL, first on forward slash, then on colon. 然后,Jq分割结果URL,首先在正斜杠上,然后在冒号上。 The result should be the hostname (or in the case of your sample data, the IP address). 结果应为主机名(对于示例数据,则为IP地址)。

Note that in your initial script, you had a quoting problem -- your variable assignment used backquotes, which are an obsolete mechanism to handle command substitution. 请注意,在您的初始脚本中,您遇到了一个引用问题-变量分配使用了反引号,这是一种过时的机制来处理命令替换。 Double or single quotes are used for strings. 双引号或单引号用于字符串。 You probably already know that by now. 您现在可能已经知道了。 ;-) ;-)

Your script has multiple problems. 您的脚本有多个问题。 The quick and dirty answer is "use double quotes" but in addition, you need to fix your hideous syntax errors. 快速而肮脏的答案是“使用双引号”,但此外,您还需要修复难看的语法错误。

#!/bin/bash
# single quotes '...' not backticks `...`
key='solr.baseuri'
# avoid useless cat
# prefer modern $(...) syntax over paleolithic `...`
# use double quotes around variable
value=$(jq ".\"$key\"" test.json | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b")

echo "solr value is $value"

It should not be hard to avoid the grep (and perhaps even also the echo ) and use jq to extract just the IP address from the URL, either. 避免grep (甚至可能还包括echo )并使用jq从URL中仅提取IP地址也不难。 ( \\b is not valid in most grep versions anyway.) \\b在大多数grep版本中都无效。)

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

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