我們從下面的字典格式串列中獲取 API 呼叫的輸出,
[
{
"Key": "/builder-deployer/test2/services/serviceA/ip",
"Val": "10.1.2.1"
},
{
"Key": "/builder-deployer/test2/services/serviceA/port",
"Val": "2"
},
{
"Key": "/builder-deployer/test2/services/serviceA/url",
"Val": "serviceA.abc.com"
},
{
"Key": "/builder-deployer/test2/services/serviceA/username",
"Val": "jenkins"
},
{
"Key": "/builder-deployer/test2/services/serviceB/ip",
"Val": "10.1.2.2"
},
{
"Key": "/builder-deployer/test2/services/serviceB/port",
"Val": "3"
},
{
"Key": "/builder-deployer/test2/services/serviceB/url",
"Val": "serviceB.abc.com"
},
{
"Key": "/builder-deployer/test2/services/serviceB/username",
"Val": "jenkins"
},
{
"Key": "/builder-deployer/test2/services/serviceC/ip",
"Val": "10.1.2.2"
},
{
"Key": "/builder-deployer/test2/services/serviceC/port",
"Val": "4"
},
{
"Key": "/builder-deployer/test2/services/serviceC/url",
"Val": "serviceC.abc.com"
},
{
"Key": "/builder-deployer/test2/services/serviceC/username",
"Val": "jenkins"
}
]
我需要從串列中以 username@ip 的形式獲取唯一的用戶名和 IP 組合以安裝一些依賴項。即在上面的字典串列中,我們有 2 個唯一的 username@ip 組合。所以我需要分別在[email protected],[email protected]機器上安裝包。
我在 python 腳本中有下面的代碼,它做同樣的事情,但我們的大部分代碼都在 Bash 中,我們也想將它轉換為 Bash。任何幫助都會非常有幫助
ip=[]
for username in outputs:
for ip in outputs:
if username['Key'].split('/')[-2] == ip['Key'].split('/')[-2] and username['Key'].split('/')[-1] in ['username'] and ip['Key'].split('/')[-1] in ['ip']:
ip.append(username['Val'] '@' ip['Val'])
print(set(ip))
uj5u.com熱心網友回復:
jq
$ FILE="file.json"
$ paste -d@ \
<(jq -r '.[]|select(.Key|contains("/username")).Val' "$FILE") \
<(jq -r '.[]|select(.Key|contains("/ip")).Val' "$FILE") | \
sort -u
[email protected]
[email protected]
awk
awk -F\" '/\/ip/{getline;ip=$4}/username/{getline;$0=$4"@"ip;print|"sort -u"}' "$FILE"
[email protected]
[email protected]
uj5u.com熱心網友回復:
評論中關于將您的解決方案留在 python 中的建議(根據 Gordon Davisson)也是我的偏好。但是,如果你真的需要一個 bash 解決方案,以下是一種選擇。請記住,如果您的 API JSON 格式隨時間發生變化,它將變得脆弱。它也不是非常有效,因為它依賴于多次讀取源 JSON。此外,兩者grep和sort也被使用:
#!/bin/bash
input_json="${1:-/tmp/bar/t.json}"
while read -r line ; do
search_str=$(grep -oB 1 "${line}" "${input_json}" \
| awk 'NR==1 {gsub(/^"/,"", $2); \
gsub(/ip",$/,"username", $2); print $2}')
user=$(grep -oA 1 "${search_str}" "${input_json}" | \
awk 'NR==2 {gsub(/"/,"", $2); print $2}')
echo "${user}@${line}"
# get unique ip addresses from source json
done < <(grep -Eo "([0-9]{1,3}[\.]){3}[0-9]{1,3}" "${input_json}" | sort -u)
樣本資料:
[
{
"Key": "/builder-deployer/test2/services/serviceA/ip",
"Val": "10.1.2.1"
},
{
"Key": "/builder-deployer/test2/services/serviceA/port",
"Val": "2"
},
{
"Key": "/builder-deployer/test2/services/serviceA/url",
"Val": "serviceA.abc.com"
},
{
"Key": "/builder-deployer/test2/services/serviceA/username",
"Val": "jenkins"
},
{
"Key": "/builder-deployer/test2/services/serviceB/ip",
"Val": "10.1.2.2"
},
{
"Key": "/builder-deployer/test2/services/serviceB/port",
"Val": "3"
},
{
"Key": "/builder-deployer/test2/services/serviceB/url",
"Val": "serviceB.abc.com"
},
{
"Key": "/builder-deployer/test2/services/serviceB/username",
"Val": "jenkins"
},
{
"Key": "/builder-deployer/test2/services/serviceC/ip",
"Val": "10.1.2.2"
},
{
"Key": "/builder-deployer/test2/services/serviceC/port",
"Val": "4"
},
{
"Key": "/builder-deployer/test2/services/serviceC/url",
"Val": "serviceC.abc.com"
},
{
"Key": "/builder-deployer/test2/services/serviceC/username",
"Val": "jenkins"
}
]
樣本輸出:
$ ./t.sh
[email protected]
[email protected]
uj5u.com熱心網友回復:
使用jq一次,不需要臨時檔案
$ cat test.json | jq -r '.[]|select(.Key|test("(username|ip)$")).Val' \
| paste -d '@' - - | sort -u
10.1.2.1@jenkins
10.1.2.2@jenkins
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/481986.html
