我正在尝试将7z文件内容列表转换为json,并且无法修复输出转换后的块之间缺少的分隔符。
我在json转换方面有点新手,但发现jq可以胜任。 我阅读了jq documentation,并在其他地方找到了示例inside here和there,但没有解决方法。
jq -f pf_7z.jq -RThe input file demo.lst:
Date Time Attr Size Compressed Name ------------------- ----- ------------ ------------ ------------------------ 2018-06-23 14:02:16 D.... 0 0 Installer 2018-06-23 14:02:16 ..... 3381 1157 Installer\Readme 2018-06-23 14:02:16 ..... 4646 1157 Installer\License.txt 2018-06-23 14:02:16 ..... 138892 136152 Installer\Setup.exe
The filter file pf7z.jq:
def parse: def parse_line: . | map(match("(\\d+-\\d+-\\d+) (\\d+:\\d+:\\d+) (D|.).* +(\\d+) +(\\d+) +(.*\\\\)([^\\\\]*)\\.(.*)")) | .[] | ({ "date" :(.captures[0].string), "time" :(.captures[1].string), "attr" :(.captures[2].string), "size" :(.captures[3].string), "path" :(.captures[5].string), "name" :(.captures[6].string), "extn" :(.captures[7].string) }); split("\n") | ( {} + (parse_line)); parse
{ “ date”:“ 2018-06-23”, “ time”:“ 14:02:16”, “ attr”:“。”, “ size”:“ 4646”, “ path”:“安装程序\”, “ name”:“许可证”, “ extn”:“ txt” }, { “ date”:“ 2018-06-23”, “ time”:“ 14:02:16”, “ attr”:“。”, “ size”:“ 138892”, “ path”:“安装程序\”, “ name”:“设置”, “ extn”:“ exe” }
{ “ date”:“ 2018-06-23”, “ time”:“ 14:02:16”, “ attr”:“。”, “ size”:“ 4646”, “ path”:“安装程序\”, “ name”:“许可证”, “ extn”:“ txt” } { “ date”:“ 2018-06-23”, “ time”:“ 14:02:16”, “ attr”:“。”, “ size”:“ 138892”, “ path”:“安装程序\”, “ name”:“设置”, “ extn”:“ exe” }
在块之间没有逗号分隔符。
谢谢;-)
答案 0 :(得分:0)
您对parse_line
的定义会生成JSON实体流,而您显然需要JSON数组。使用正则表达式,您可以编写:
def parse:
def parse_line:
match("(\\d+-\\d+-\\d+) (\\d+:\\d+:\\d+) (D|.).* +(\\d+) +(\\d+) +(.*\\\\)([^\\\\]*)\\.(.*)")
| .captures
| map(.string)
| { "date" :.[0],
"time" :.[1],
"attr" :.[2],
"size" :.[3],
"path" :.[5],
"name" :.[6],
"extn" :.[7] } ;
[inputs | parse_line];
parse
jq -nR -f 7z.jq 7z.txt
正则表达式片段(D|.).*
没有多大意义。
您应该考虑将其替换为(.)[^ ]*
或类似的内容。
def parse_line:
capture("(?<date>\\d+-\\d+-\\d+) "
+ "(?<time>\\d+:\\d+:\\d+) "
+ "(?<attr>.)[^ ]* +"
+ "(?<size>\\d+) +\\d+ +"
+ "(?<path>.*\\\\)"
+ "(?<name>[^\\\\]*)\\."
+ "(?<extn>.*)");
[inputs | parse_line]
从关于JSONEdit的评论中,我看来您的总体方法可能不是最佳的。您是否考虑过在JSONEdit中使用jq而不是jq?