我在一个目录中有 20 多个 json 文件,每个文件都有一个属性“userType”,其值可以是 customer 或 employee,我需要在这里创建 2 个 zip customer.zip,它将包含所有包含文本“usertype”的文件: employee.zip 中的“客户”和剩余的 json
我可以 grep 获取文件名列表的文本:
grep \"userType\":\"customer\" *.json
这给了我 json 文件的列表,我应该如何将这个列表传递给 zip 创建命令
最佳答案
将 grep -l
与 xargs
一起使用:
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/