系统
\n\nLinux 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
或其他应用程序,我只需要 bashsed
或awk
,或一些基本命令,我没有安装它的管理员权限。
我试过
\n\n我尝试过cut
,但是分隔符有问题,
(有时在"
)。
谢谢。
\n你可以试试这个:
\n\ngrep -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\ngrep -o \'"[^"]*"\\s*:\\s*"[^"]*"\'
找到所有"key": "value"
对并将它们打印在单独的行上;例子:
\n\necho \'{ "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\ngrep -E \'^"(code|name|is_action|action_from|action_to)"\'
仅过滤需要的键。输出:
\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\ntr \'\\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\nsed \'s/,$//\'
删除最后一个多余的逗号。输出:
\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\necho \'{ "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