I'm searching every txt files that contains string1 and string2
grep -rnE --include='*.txt' 'string1|string2'
Now I want to put all those files into a zip file.
Thanks
CodePudding user response:
You can use -l
option in grep
to output only filename and pipe it to xargs
to create zip
for each found fine:
grep --null -rlE --include='*.txt' 'string1|string2' |
xargs -0 -I {} zip '{}'.zip '{}'
Note use of --null
option with -0
in xargs
to address filenames with whitespaces, special characters.
CodePudding user response:
since you wanted files that contain both string1 and string2
$ files=""; for i in *.txt; do if grep -q string1 $i && grep -q string2 $i; then files="$files $i"; fi; done
$ zip files.zip ${files}
cleaner version
files="";
for i in *.txt;
do
if grep -q string1 $i && grep -q string2 $i; then
files="$files $i";
fi;
done
zip files.zip ${files}