我有相当多的 JSON 文件,它们具有不同的版本作为属性。我想修改仅与特定谓词匹配的文件。这是使用 find 和 jq 进行就地编辑的后续操作。
这对我来说很重要,因为如果我修改数百个文件并打开 PR,除了我想要更改的内容之外,我不需要任何其他内容。例如,修改缩进看起来整个文件都被修改了,审阅者很难看到实际的差异。
为了说明我的意思,请将以下两个文件放入名为 的文件夹中json
:
{
"identifier": "1",
"version" : "1.0"
}
Run Code Online (Sandbox Code Playgroud)
请注意版本后面的尾随空格,这是故意的。
{
"identifier": "2",
"version": "2.0"
}
Run Code Online (Sandbox Code Playgroud)
这些片段中的缩进为 4 个空格(vscode 中默认)。
运行指向 json 文件夹的以下脚本:
find json \
-name '*.json' \
-type f -exec sh -c '
tmpfile=$(mktemp)
for pathname do
cp -- "$pathname" "$tmpfile" &&
jq "select(.version == \"2.0\").identifier |= \"\"" <"$tmpfile" >"$pathname"
done
rm -f "$tmpfile"' sh {} +
Run Code Online (Sandbox Code Playgroud)
我期望的结果是,只有具有版本的文件2.0
才会被修改,除了identifier
. 最终结果是这样的:
第一个文件具有原始标识符(预期),但缩进现在为 2 …
总体而言,我对 Linux 非常陌生,我正在尝试找到一种在 Alpine docker 映像上构建 Debian 软件包的方法。这是迄今为止我可以让管道运行的最快速度,但不幸的是,当我尝试运行时dpkg-deb --build
,出现以下错误:
03:22:44 dpkg-deb: building package 'x-sync' in 'x-sync_1.0.2.deb'.
03:22:44 tar: unrecognized option: format=gnu
03:22:44 BusyBox v1.27.2 (2018-01-29 15:48:57 GMT) multi-call binary.
03:22:44
03:22:44 Usage: tar -[cxtZzJjahmvO] [-X FILE] [-T FILE] [-f TARFILE] [-C DIR] [FILE]...
03:22:44
03:22:44 Create, extract, or list files from a tar file
03:22:44
03:22:44 Operation:
03:22:44 c Create
03:22:44 x Extract
03:22:44 t List
03:22:44 f Name of TARFILE ('-' for stdin/out)
03:22:44 C Change to …
Run Code Online (Sandbox Code Playgroud)