user1731553
user1731553

Reputation: 1937

Create a zip of files containing a particular text

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

Answers (1)

anubhava
anubhava

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

Related Questions