Reputation: 1937
I have 20+ json files in a directory each have an attribute "userType" whose value can be customer or employee, I need to create 2 zips here customer.zip that will contain all the files that has text "usertype":"customer" and remaining json in employee.zip
I can grep for the text to get the list of file names:
grep \"userType\":\"customer\" *.json
this gives me the list of json files, how should I pass this list to the zip creation command
Upvotes: 1
Views: 70
Reputation: 785098
Use grep -l
with xargs
:
grep -lZ '"userType":"customer"' *.json | xargs -0 zip customer.zip
and
grep -lZ '"userType":"employee"' *.json | xargs -0 zip employee.zip
PS: -Z
option in grep
outputs a NUL byte after each filename and xargs -0
reads a NUL delimited input. This is used to take care of filenames with spaces, newlines and glob characters.
Upvotes: 2