英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论