I have two files with JSON lists:
File 1
[0,1,2]
["a", "b", "c"]
File 2
[3,4,5]
["d", "e", "f"]
How can I use jq
or a similar command line tool to combine the lists in each line? The result from combining the two files above should be :
[0,1,2,3,4,5]
["a", "b", "c", "d", "e", "f"]
Read both files into an array, use transpose
to combine by their index, and perform the operation on the resulting array, i.e. add
the subarrays.
jq -nc --slurpfile f1 file1.json --slurpfile f2 file2.json \
'[$f1, $f2] | transpose[] | add'
Alternatively, read them into one array, and split that single array in half to produce the two subarrays:
jq -sc '[.[:length/2], .[length/2:]] | transpose[] | add' file1.json file2.json
Output:
[0,1,2,3,4,5]
["a","b","c","d","e","f"]