I have a few .json
files in my directory. If I do:
find . -name '*.json' -exec echo "{\"filename\": \"{}\", \"content\": `cat {}` }," \;
I get:
{"filename": "./a.json", "content": },
{"filename": "./b.json", "content": },
However, if I do:
find . -name '*.json' -exec echo "{\"filename\": \"{}\", \"content\": cat {} }," \;
I get:
{"filename": "./a.json", "content": cat ./a.json },
{"filename": "./b.json", "content": cat ./b.json },
so how do I make the a.json
and b.json
contents be cat
ed correctly ?
BTW, if I do:
find . -name '*.json' -exec cat {} \;
the json files are correctly printed in the console, so I know that the file contents are valid.
With simple bash :
$ cat a.json
{ "a":123 }
$ cat b.json
{ "b":234 }
$ cat "foo/bar/base/c c.json"
{ "c": 456 }
$ shopt -s globstar # enable recursion with '**'
$ for i in **/*.json; do
cat<<EOF
{ "filename": "$i", "content": $(cat "$i") }
EOF
done | tee file.json
$ cat file.json
{ "filename": "a.json", "content": {"a":123} }
{ "filename": "b.json", "content": {"b":234} }
{ "filename": "foo/bar/base/c c.json", "content": { "c": 456 } }