Bash,从 json 行获取值

gen*_*bee 1 bash sed awk json

系统

\n\n
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\n
Run Code Online (Sandbox Code Playgroud)\n\n

问题

\n\n

我有输入文件。

\n\n
{ "article": {"code": "01333457004","name": "ALAZANIS VALLEY 2015","note": "\xc4\x8cERV V\xc3\x8dNO EVROPA V\xc3\x9dCH OSTATN\xc3\x8d","sel_unit": "Kus","unit_price": 229.0,"category": "\xc4\x8cERVEN\xc3\x89,POLOSLADK\xc3\x89","unit": "L","EAN": "4867601700052","unit_volume": 0.75,"producer": null,"tax": 21.0,"text": "Alazanis Valley 2015;Gruzie,Kachetie;\xc4\x8derven\xc3\xa9 polsladk\xc3\xa9;750ml;16\xc2\xb0C;Alazanis Valley 2015;Gruzie,Kachetie;\xc4\x8derven\xc3\xa9 polsladk\xc3\xa9;750ml;16\xc2\xb0C;","is_action": "1","action_from": "20190905","action_to": "20190918","ordered_from": "20190126","ordered_to": "20190830","shelf_id": "1030542","is_outlet": 0}}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我怎样才能得到这个输出(一些列)?

\n\n
"code": "01333457004","name": "ALAZANIS VALLEY 2015","is_action": "1","action_from": "20190905","action_to": "20190918"\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

我试过

\n\n

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

\n\n

谢谢。

\n

aro*_*pan 5

你可以试试这个:

\n\n
grep -o \'"[^"]*"\\s*:\\s*"[^"]*"\' | \\\ngrep -E \'^"(code|name|is_action|action_from|action_to)"\' | \\\ntr \'\\n\' \',\' | \\\nsed \'s/,$//\'\n
Run Code Online (Sandbox Code Playgroud)\n\n

细节

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

例子:

\n\n
echo \'{ "article": {"code": "01333457004","name": "ALAZANIS VALLEY 2015","note": "\xc4\x8cERV V\xc3\x8dNO EVROPA V\xc3\x9dCH OSTATN\xc3\x8d","sel_unit": "Kus","unit_price": 229.0,"category": "\xc4\x8cERVEN\xc3\x89,POLOSLADK\xc3\x89","unit": "L","EAN": "4867601700052","unit_volume": 0.75,"producer": null,"tax": 21.0,"text": "Alazanis Valley 2015;Gruzie,Kachetie;\xc4\x8derven\xc3\xa9 polsladk\xc3\xa9;750ml;16\xc2\xb0C;Alazanis Valley 2015;Gruzie,Kachetie;\xc4\x8derven\xc3\xa9 polsladk\xc3\xa9;750ml;16\xc2\xb0C;","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*"[^"]*"\'\n
Run Code Online (Sandbox Code Playgroud)\n\n

输出:

\n\n
"code": "01333457004"\n"name": "ALAZANIS VALLEY 2015"\n"note": "\xc4\x8cERV V\xc3\x8dNO EVROPA V\xc3\x9dCH OSTATN\xc3\x8d"\n"sel_unit": "Kus"\n"category": "\xc4\x8cERVEN\xc3\x89,POLOSLADK\xc3\x89"\n"unit": "L"\n"EAN": "4867601700052"\n"text": "Alazanis Valley 2015;Gruzie,Kachetie;\xc4\x8derven\xc3\xa9 polsladk\xc3\xa9;750ml;16\xc2\xb0C;Alazanis Valley 2015;Gruzie,Kachetie;\xc4\x8derven\xc3\xa9 polsladk\xc3\xa9;750ml;16\xc2\xb0C;"\n"is_action": "1"\n"action_from": "20190905"\n"action_to": "20190918"\n"ordered_from": "20190126"\n"ordered_to": "20190830"\n"shelf_id": "1030542"\n
Run Code Online (Sandbox Code Playgroud)\n\n
    \n
  • grep -E \'^"(code|name|is_action|action_from|action_to)"\'仅过滤需要的键。
  • \n
\n\n

输出:

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

输出:

\n\n
"code": "01333457004","name": "ALAZANIS VALLEY 2015","is_action": "1","action_from": "20190905","action_to": "20190918",\n
Run Code Online (Sandbox Code Playgroud)\n\n
    \n
  • sed \'s/,$//\'删除最后一个多余的逗号。
  • \n
\n\n

输出:

\n\n
"code": "01333457004","name": "ALAZANIS VALLEY 2015","is_action": "1","action_from": "20190905","action_to": "20190918"\n
Run Code Online (Sandbox Code Playgroud)\n\n

完整示例

\n\n

命令:

\n\n
echo \'{ "article": {"code": "01333457004","name": "ALAZANIS VALLEY 2015","note": "\xc4\x8cERV V\xc3\x8dNO EVROPA V\xc3\x9dCH OSTATN\xc3\x8d","sel_unit": "Kus","unit_price": 229.0,"category": "\xc4\x8cERVEN\xc3\x89,POLOSLADK\xc3\x89","unit": "L","EAN": "4867601700052","unit_volume": 0.75,"producer": null,"tax": 21.0,"text": "Alazanis Valley 2015;Gruzie,Kachetie;\xc4\x8derven\xc3\xa9 polsladk\xc3\xa9;750ml;16\xc2\xb0C;Alazanis Valley 2015;Gruzie,Kachetie;\xc4\x8derven\xc3\xa9 polsladk\xc3\xa9;750ml;16\xc2\xb0C;","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/,$//\'\n
Run Code Online (Sandbox Code Playgroud)\n\n

输出:

\n\n
"code": "01333457004","name": "ALAZANIS VALLEY 2015","is_action": "1","action_from": "20190905","action_to": "20190918"\n
Run Code Online (Sandbox Code Playgroud)\n

  • @SimonSudler 你的评论呼应了我几天前的想法。答案已修改,我已投票。 (2认同)