linux - 创建包含特定文本的文件压缩包

标签 linux shell

我在一个目录中有 20 多个 json 文件,每个文件都有一个属性“userType”,其值可以是 customer 或 employee,我需要在这里创建 2 个 zip customer.zip,它将包含所有包含文本“usertype”的文件: employee.zip 中的“客户”和剩余的 json

我可以 grep 获取文件名列表的文本:

grep \"userType\":\"customer\" *.json

这给了我 json 文件的列表,我应该如何将这个列表传递给 zip 创建命令

最佳答案

grep -lxargs 一起使用:

grep -lZ '"userType":"customer"' *.json | xargs -0 zip customer.zip

grep -lZ '"userType":"employee"' *.json | xargs -0 zip employee.zip

PS:grep 中的 -Z 选项在每个文件名后输出一个 NUL 字节,而 xargs -0 读取一个 NUL 分隔输入。这用于处理带有空格、换行符和 glob 字符的文件名。

关于linux - 创建包含特定文本的文件压缩包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52954417/

相关文章:

linux - Bash:将 getops 用于此特定用途

c - 退出程序后输出执行UNIX命令

从 Bash 调用 Java 时无法执行二进制文件

java - 在 linux 中使用 ProcessBuilder 执行 shell 命令

Linux:用列格式化流(管道)输出? (未确定的文件大小)

java - 无法在redhat中将tomcat作为服务启动

linux - 将 pattern1 和 pattern2 之间的行放入一个单独的文件中,包括 shell 中带有 pattern1 的行

linux - 在 ls 输出中包括换行符?

linux - Bash 解析和 shell 扩展

linux - 为读取系统调用编写 Haskell FFI