Bash,从 json 行获取值

Bash,从 json 行获取值

系统

Linux local 5.0.0-27-lowlatency #28-Ubuntu SMP PREEMPT Tue Aug 20 20:33:37 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

问题

我有输入文件。

{ "article": {"code": "01333457004","name": "ALAZANIS VALLEY 2015","note": "ČERV VÍNO EVROPA VÝCH OSTATNÍ","sel_unit": "Kus","unit_price": 229.0,"category": "ČERVENÉ,POLOSLADKÉ","unit": "L","EAN": "4867601700052","unit_volume": 0.75,"producer": null,"tax": 21.0,"text": "Alazanis Valley 2015;Gruzie,Kachetie;červené polsladké;750ml;16°C;Alazanis Valley 2015;Gruzie,Kachetie;červené polsladké;750ml;16°C;","is_action": "1","action_from": "20190905","action_to": "20190918","ordered_from": "20190126","ordered_to": "20190830","shelf_id": "1030542","is_outlet": 0}}

我怎样才能仅获取此输出(某些列)?

"code": "01333457004","name": "ALAZANIS VALLEY 2015","is_action": "1","action_from": "20190905","action_to": "20190918"

没有jq或其他应用程序,我只需要 bashsedawk,或一些基本命令,我没有管理员权限来安装它。

我试过

我试过了cut,但是分隔符有问题,(有时在")。

谢谢。

答案1

您可以尝试以下操作:

grep -o '"[^"]*"\s*:\s*"[^"]*"' | \
grep -E '^"(code|name|is_action|action_from|action_to)"' | \
tr '\n' ',' | \
sed 's/,$//'

细节

  • grep -o '"[^"]*"\s*:\s*"[^"]*"'找到所有"key": "value"对并将它们打印在不同的行上;

例子:

echo '{ "article": {"code": "01333457004","name": "ALAZANIS VALLEY 2015","note": "ČERV VÍNO EVROPA VÝCH OSTATNÍ","sel_unit": "Kus","unit_price": 229.0,"category": "ČERVENÉ,POLOSLADKÉ","unit": "L","EAN": "4867601700052","unit_volume": 0.75,"producer": null,"tax": 21.0,"text": "Alazanis Valley 2015;Gruzie,Kachetie;červené polsladké;750ml;16°C;Alazanis Valley 2015;Gruzie,Kachetie;červené polsladké;750ml;16°C;","is_action": "1","action_from": "20190905","action_to": "20190918","ordered_from": "20190126","ordered_to": "20190830","shelf_id": "1030542","is_outlet": 0}}' | grep -o '"[^"]*"\s*:\s*"[^"]*"'

输出:

"code": "01333457004"
"name": "ALAZANIS VALLEY 2015"
"note": "ČERV VÍNO EVROPA VÝCH OSTATNÍ"
"sel_unit": "Kus"
"category": "ČERVENÉ,POLOSLADKÉ"
"unit": "L"
"EAN": "4867601700052"
"text": "Alazanis Valley 2015;Gruzie,Kachetie;červené polsladké;750ml;16°C;Alazanis Valley 2015;Gruzie,Kachetie;červené polsladké;750ml;16°C;"
"is_action": "1"
"action_from": "20190905"
"action_to": "20190918"
"ordered_from": "20190126"
"ordered_to": "20190830"
"shelf_id": "1030542"
  • grep -E '^"(code|name|is_action|action_from|action_to)"'仅过滤需要的键。

输出:

"code": "01333457004"
"name": "ALAZANIS VALLEY 2015"
"is_action": "1"
"action_from": "20190905"
"action_to": "20190918"
  • tr '\n' ','将新行替换为逗号。

输出:

"code": "01333457004","name": "ALAZANIS VALLEY 2015","is_action": "1","action_from": "20190905","action_to": "20190918",
  • sed 's/,$//'删除最后一个多余的逗号。

输出:

"code": "01333457004","name": "ALAZANIS VALLEY 2015","is_action": "1","action_from": "20190905","action_to": "20190918"

完整示例

命令:

echo '{ "article": {"code": "01333457004","name": "ALAZANIS VALLEY 2015","note": "ČERV VÍNO EVROPA VÝCH OSTATNÍ","sel_unit": "Kus","unit_price": 229.0,"category": "ČERVENÉ,POLOSLADKÉ","unit": "L","EAN": "4867601700052","unit_volume": 0.75,"producer": null,"tax": 21.0,"text": "Alazanis Valley 2015;Gruzie,Kachetie;červené polsladké;750ml;16°C;Alazanis Valley 2015;Gruzie,Kachetie;červené polsladké;750ml;16°C;","is_action": "1","action_from": "20190905","action_to": "20190918","ordered_from": "20190126","ordered_to": "20190830","shelf_id": "1030542","is_outlet": 0}}' | grep -o '"[^"]*"\s*:\s*"[^"]*"' | grep -E '^"(code|name|is_action|action_from|action_to)"' | tr '\n' ',' | sed 's/,$//'

输出:

"code": "01333457004","name": "ALAZANIS VALLEY 2015","is_action": "1","action_from": "20190905","action_to": "20190918"

答案2

#!/bin/bash

declare -A JSON
a=$(
  grep -Po '\{.*\{\K(.*?)(?=\}\})' example.json |
  grep -Po '(^|,)\"\K(.*?)(?=\":)'
)
for b in $a; do
  JSON[$b]="$(grep -Po "\"$b\": \"?\K.*?(?=(\"?,\"|\}\}))" example.json)"
done


echo ${JSON[@]}
echo ${!JSON[@]}
echo ${JSON[name]}

或者如果你只想要这些值:

grep -Po '\{.*\{\K(.*?)(?=\}\})' example.json | grep -Po "\".*?\": \"?\K.*?(?=(\"?,\"|$))"

答案3

尽管,分隔符失败,但是您可以使用:分隔符cut

#!/bin/bash

# NAME: ParseJSON
# PATH: $HOME/askubuntu/
# DESC: Answer for: https://askubuntu.com/questions/1174505/bash-get-value-from-json-line
# DATE: September 16, 2019

Line='{ "article": {"code": "01333457004","name": "ALAZANIS VALLEY 2015","note": "ČERV VÍNO EVROPA VÝCH OSTATNÍ","sel_unit": "Kus","unit_price": 229.0,"category": "ČERVENÉ,POLOSLADKÉ","unit": "L","EAN": "4867601700052","unit_volume": 0.75,"producer": null,"tax": 21.0,"text": "Alazanis Valley 2015;Gruzie,Kachetie;červené polsladké;750ml;16°C;Alazanis Valley 2015;Gruzie,Kachetie;červené polsladké;750ml;16°C;","is_action": "1","action_from": "20190905","action_to": "20190918","ordered_from": "20190126","ordered_to": "20190830","shelf_id": "1030542","is_outlet": 0}}'

Code=$(echo "$Line" | cut -d':' -f3)
Code="${Code%,*}"                           # Remove ,"name"
Name=$(echo "$Line" | cut -d':' -f4)
Name="${Name%,*}"                           # Remove ,"note"
IsAction=$(echo "$Line" | cut -d':' -f15)
IsAction="${IsAction%,*}"                   # Remove ,"action_from"
ActionFrom=$(echo "$Line" | cut -d':' -f16)
ActionFrom="${ActionFrom%,*}"               # Remove ,"action_to"
ActionTo=$(echo "$Line" | cut -d':' -f17)
ActionTo="${ActionTo%,*}"                   # Remove ,"ordered_from"

echo '"code":'"$Code,"'"name":'"$Name,"'"is_action":'"$IsAction,"'"action_from":'"$ActionFrom,"'"action_to":'"$ActionTo"

我调用了该脚本ParseJSON,这是它的输出:

$ ParseJSON

"code": "01333457004","name": "ALAZANIS VALLEY 2015","is_action": "1","action_from":  "20190905","action_to": "20190918"

相关内容