Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

/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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading