使用jq处理文件列表并将输出保存到文件。

huangapple go评论88阅读模式
英文:

processing a list of files using jq and saving output to file

问题

我有一个文件列表,如下所示:

/path/to/dir1/f1.json
/path/to/dir2/f1.json
/path/to/dir1/f12.json
..

JSON 数据如下:

{"score": 0.98}

我想使用 jq 处理所有这些文件并生成以下输出:

/path/to/dir1/f1.json,0.76
/path/to/dir2/f1.json,0.89
/path/to/dir1/f12.json,0.98
..

我知道如何在单个文件上使用 jq,但希望能够自动化处理文件列表并生成上述输出,如果可能的话。

有关如何正确使用管道将数据传递给 jq 并组装输出的提示将非常有帮助。

英文:

I have a list of files like so:

/path/to/dir1/f1.json
/path/to/dir2/f1.json
/path/to/dir1/f12.json
..

the json is like so:

{"score": 0.98}

I would like to use jq to process all these files and produce an output:

/path/to/dir1/f1.json,0.76
/path/to/dir2/f1.json,0.89
/path/to/dir1/f12.json,0.98
..

I know how to use jq on a single file, but would like to automate this for the list of files and produce the above output if this is possible.

Any tips on how to pipe correctly to jq and assemble the output would be great

答案1

得分: 1

使用input_filename获取文件名,字符串插值"\(...)"组合,-r标志输出文本:

jq -r ''"\(input_filename),\(.score)"'' /path/to/*/*.json
/path/to/dir1/f12.json,0.98
/path/to/dir1/f1.json,0.76
/path/to/dir2/f1.json,0.89
英文:

Use input_filename to get the file names, string interpolation "\(...)" to compose and the -r flag to output text:

jq -r '"\(input_filename),\(.score)"' /path/to/*/*.json
/path/to/dir1/f12.json,0.98
/path/to/dir1/f1.json,0.76
/path/to/dir2/f1.json,0.89

huangapple
  • 本文由 发表于 2023年6月12日 23:30:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458159.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定