【发布时间】:2020-09-21 04:36:31
【问题描述】:
我的目录中有几个.json 文件。如果我这样做:
find . -name '*.json' -exec echo "{\"filename\": \"{}\", \"content\": `cat {}` }," \;
我明白了:
{"filename": "./a.json", "content": },
{"filename": "./b.json", "content": },
但是,如果我这样做:
find . -name '*.json' -exec echo "{\"filename\": \"{}\", \"content\": cat {} }," \;
我明白了:
{"filename": "./a.json", "content": cat ./a.json },
{"filename": "./b.json", "content": cat ./b.json },
那么如何使a.json 和b.json 内容正确cated?
顺便说一句,如果我这样做:
find . -name '*.json' -exec cat {} \;
json文件在控制台打印正确,所以我知道文件内容是有效的。
【问题讨论】:
-
不要尝试通过连接字符串来创建 JSON。使用
jq实用程序。 -
我很乐意接受
jq的回答,因为我正在尝试做的事情。 -
@Inian 会在多个子目录中,文件名也会包含空格。