Below is a snippet of a for loop where I sort txt file names. I am then trying to save the results in a json format file. However it results in an json format that is not the desired. How could i convert to desired json format the values from the for loop?
dir="myfiles/test/"
prefix=""
echo "[" >> test.json
for dir in "${array[@]}"; do
#reverse the result of comparisons
file=$(find "$dir" -maxdepth 1 -type f -iname '*.txt' | awk "NR==$i")
[[ -n $file ]] &&
printf '%b{ "filepath": "%s" }' $prefix "$file" >> test.json
prefix=",\n"
done
echo
echo "]" >> test.json
Current output
[
{ "filepath" : "myfiles/test/sdfsd.txt" },
{ "filepath" : "myfiles/test/piids.txt" },
{ "filepath" : "myfiles/test/saaad.txt" },
{ "filepath" : "myfiles/test/smmnu.txt" },
]
Desired output
[
[
{ "filepath" : "myfiles/test/sdfsd.txt" }
],
[
{ "filepath" : "myfiles/test/piids.txt" }
],
[
{ "filepath" : "myfiles/test/saaad.txt" }
],
[
{ "filepath" : "myfiles/test/smmnu.txt" }
]
]
Also allow
[
[
{ "filepath" : "myfiles/test/sdfsd.txt" },
{ "filepath" : "myfiles/test/sdfsd2.txt" }
],
[
{ "filepath" : "myfiles/test/piids.txt" },
{ "filepath" : "myfiles/test/piids2.txt" }
],
[
{ "filepath" : "myfiles/test/saaad.txt" }
],
[
{ "filepath" : "myfiles/test/smmnu.txt" }
]
]