Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find pattern inside file inside a 7z file [closed]

Tags:

linux

7zip

I have thousands of 7z files named data1.7z, data2.7z, and so on. Each file's content is data1.log, data2.log, etc. I can list the contents using 7z l *.7z. I would like to do is grep or find a pattern inside .log files without the need of extracting all. These files contain IP addresses and hoping to find a set of IP addresses from another file named ipaddress.txt within each .log file and print out the lines which match for any of the IP from the .txt file.

Thanks in advance.

like image 269
Dinesh Bhardwaj Avatar asked Mar 22 '26 02:03

Dinesh Bhardwaj


1 Answers

That is not possible. You can't grep compressed data.
Unlike the names of compressed files which 7z stores uncompressed, the actual file data is stored in a modified state (changed by the compression process).

If you want to search it, you will have to decompress it.

What you can do, is automate this process with a script, so as not to leave uncompressed files lying around.

For example, 7z can output to standard output so you can pipe the results directly to grep:

for FILE in *.7z; do
    7z e -so ${FILE} | grep '127\.0\.0\.1'
done

Alternatively, you may want to extract the file normally, then delete it if grep found nothing, or leave it uncompressed for further inspection if grep did find the IP you where looking for:

for FILE in *.7z; do
    7z e ${FILE}
    LOG=${FILE%7z}log
    grep -q '127\.0\.0\.1' ${LOG}
    if [ $? -ne 0 ]; then
        rm ${LOG}
    fi
done
like image 190
Lev M. Avatar answered Mar 24 '26 22:03

Lev M.