web-dev-qa-db-ja.com

バッシュ、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や他のアプリはありません。bashsedまたはawk、またはいくつかの基本的なコマンドが必要です。インストールするための管理者権限がありません。

試してみました

cutを試しましたが、区切り文字,に問題があります("に含まれる場合があります)。

ありがとう。

1
genderbee

あなたはこれを試すことができます:

_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
aropan
#!/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.*?(?=(\"?,\"|$))"
0
bac0n

,区切り文字は失敗しますが、:区切り文字を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"
0