我有几个 zip 文件,每个文件里面都有几个文本文件。我想用 替换a
这些文本文件中所有出现的b
,并将它们压缩为相同的结构。
如果我只想找到 的出现a
,我知道我可以使用该zgrep
命令,但我也想执行字符串替换。不幸的是,没有zsed
命令。
我别无选择,只能写一个带有循环的脚本吗?
您可以将 zip 文件挂载到目录,然后您可以像浏览和修改目录一样浏览和修改它。
安装fuse-zip(在所有好的发行版中都可以通过包管理器获得)。
mount_point=$(TMPDIR=$PWD mktemp -d)
# Iterate over the .zip files in the current directory
for z in *.zip; do
fuse-zip "$z" "$mount_point"
# At this point, the zip contents are available through the mount point
find "$mount_point" -type f -exec sed -i 's/a/b/g' {} +
fusermount -u "$mount_point"
done
rmdir "$mount_point"
Run Code Online (Sandbox Code Playgroud)
(注意:在这个例子中我假设 GNU sed 语法。)