0

I have a 7z file that contains binary data and both large (>1Mb) and small (<1Mb) XML files.

I want to find only the small XML files that contain a specific string. I've tried using zgrep, but it seems to search inside all files.

Edit: What I ended up doing was writing a list of all xml files inside the .7z, grep only the lines with 6 digits numbers and copy only the filenames and extract them. Is there a more efficient way?

7z l test.7z *.xml > tmplist1
num="6"
grep -E "\s[0-9]{$num}\s" tmplist1 | grep -oE '[^[:space:]]+$' | sed "s/^/*/" > tmplist2
7z e -o/tmp test.7z -i@tmplist2 -aos
grep -E "*string*" /tmp/*.xml > tmplist3

1 Answer 1

-1

What about

find . -iname "*.7z" -o -iname "*.xml" -size -1M -exec grep -options {} \;

? find finds block devices, special unbuffered character entries, directories, files, symlinks, named FIFO-pipes, sockets, …
. in the current working directory (not needed if it's the only path given)
-iname "*.xml" with .xml at the end of the name (case insensitive)
-o OR (logically)
-iname "*.7z" with .7z or .7Z at the end of the name
-size -1M with a size smaller than 1 MB
-exec grep -options {} and executes grep on all of the files found one by one with your own options replacing -options.
\; finally ends the -exec part.

3
  • this find 7z or xml files that are smaller than 1M, not small xml files inside 7z
    – phuclv
    Jun 17, 2023 at 13:58
  • You could unpack and search then in it…
    – Nepumuk
    Jun 17, 2023 at 15:17
  • that's not what this question asked. Sometimes extracting is not efficient. Why extracting all the huge files and waste time/memory/disk unnecessarily? That's why things like zgrep exists. And even in the case extracting is accepted then your answer is wrong because it greps on the original 7z files as well. If you include methods to extract only the required files and grep on them then it'll be more appropriate
    – phuclv
    Jun 18, 2023 at 2:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .