Genivia / ugrep

NEW ugrep 6.5: a more powerful, ultra fast, user-friendly, compatible grep. Includes a TUI, Google-like Boolean search with AND/OR/NOT, fuzzy search, hexdumps, searches (nested) archives (zip, 7z, tar, pax, cpio), compressed files (gz, Z, bz2, lzma, xz, lz4, zstd, brotli), pdfs, docs, and more
https://ugrep.com
BSD 3-Clause "New" or "Revised" License
2.56k stars 109 forks source link

How to show file name inside a tar file #359

Closed huornlmj closed 6 months ago

huornlmj commented 6 months ago

I'm searching for a phrase inside files that are themselves inside tar files which are scattered throughout a number of sub directories.

$ ugrep -rl --include="*.tar" 'phrase' .

What I get in output is:

dir1/tarfile.tar
dir2/othertarfile.tar
dir2/anothertar.tar
dir3/tartar.tar

What I want is not to see the name of the file inside the tar file that the phrase was in. For example:

dir1/tarfile.tar has a file in it called /home/user/thefile.txt and it contains the phrase "phrase" that I'm searching for. I want to see the name of the "/home/user/thefile.txt" as well as the name of the dir1/tarfile.tar file.

genivia-inc commented 6 months ago

If you only want to recursively search for tar files and then list the files in those that match a pattern:

ug -rl -Otar "" | xargs ug -lz "PATTERN"

This runs a ugrep to simply look for tar files recursively with ug -rl -Otar "" and a second ugrep to list each of the tar contents with ug -lz "PATTERN" to match PATTERN in files stored in tar files. I am using ug here to pretty print and sort the tar files (option --sort). With ugrep use the following to sort tar files by name (or specify a sort key):

ugrep --sort -rl -Otar "" | xargs ugrep -lz "PATTERN"

Note that the problem with using --include="*.tar" (same as -Otar) is that you only select tar files to search, which also only searches tar files inside tar files when you use option -z. Option -z must be used to search inside tar, tarballs, zip files and compressed files.

genivia-inc commented 6 months ago

Did that work for you? If so, can this be closed?

Using xargs for this scenario is not uncommon, because it involves two conflicting search criteria, one to find tar files and the other to search the tar files.

huornlmj commented 6 months ago

Yes that worked perfectly! Many thanks. Closing now.