Grepping within a gzipped tarball



Published: 2021-10-04 19:16:41 +0000
Categories: BASH,

Language

BASH

Description

You've got a (presumably) large tarball that you want to grep within, but don't want to extract the thing to do so

What you want to know, is which files within the archive contain a specific pattern so that you can extract just those one

This snippet details how to run grep against files within a tarball

Snippet

tar xzf [tarball] --to-command 'grep -H --label="$TAR_FILENAME" [grep options] '[pattern]'; true'

Usage Example

# Build a tarball to search
echo abcdedfg | tee not-a.txt
fortune | cowsay | tee not-b.txt
echo abcdefg0123456aa |  tee not-c.txt
echo 01221665889463 |  tee is-d.txt
echo 0118 999 881 999 119 725 3 | tee is-e.txt
tar czf files.tar.gz *txt

# Run the grep - anything that has lines starting with numbers
tar xzf files.tar.gz --to-command 'grep -H --label="$TAR_FILENAME" -P '^[0-9]+'; true'
is-d.txt:01221665889463
is-e.txt:0118 999 881 999 119 725 3

Keywords

grep, tar, gzip, archive, tarball, bash,

Latest Posts


Copyright © 2021 Ben Tasker | Sitemap | Privacy Policy
Available at snippets.bentasker.co.uk and http://phecoopwm6x7azx26ctuqcp6673bbqkrqfeoiz2wwk36sady5tqbdpqd.onion