0

I would like to know how to display the filename along with the lines matching a specfic word of a tar file.

Command wise :

zcat file | grep "stuff" -r # shows what I want
zcat *.gz | grep "stuff" -ar # this fails
3
  • Welcome to SO! Please consider formatting your code with the { } button to increase readability . Apr 17, 2015 at 15:10
  • 1
    check out stackoverflow.com/questions/13983365/…
    – VF_
    Apr 17, 2015 at 15:15
  • That button is in the editor, don't type { and } :-) You can just put 4 spaces before each line alternatively. Apr 17, 2015 at 15:35

5 Answers 5

0

You can use zgrep:

For single file, you can use the following command to display filename:

zgrep "stuff" file.gz /dev/null

For multiple files:

zgrep "stuff" *.gz 
2
  • It looks like the OP has a tgz. while zgrep does a good job grepping inside gzipped files, I don't think it will work well for grepping in files inside a gzipped tarball and print their name. Apr 17, 2015 at 15:18
  • Yeah I know, but the OP also wrote "tar file". Maybe I'm wrong and they meant "gz file". Apr 17, 2015 at 15:53
0

Maybe this related answer can help. It uses tar to untar (you would need to add -z) and pipes each file of the archive to awk for "grepping" inside it.

0

I'm not quite sure what the question is but if you are looking for tar files on your system then just do something like this. This will recursively search your current directory and any child directories for .tar files. Hope this helps.

find -name "*.tar"
3
  • No, it willl not - did you mean find? The OP does not want to look for tar files, but grep for an expression inside files in a tar file. Apr 17, 2015 at 15:25
  • oops, yes I did I meant: find -name "*.tar" Apr 17, 2015 at 15:28
  • Now that statement is correct, thanks for editing. I'm just afraid that your post still does not actually answer the question :-) Apr 17, 2015 at 15:34
0

If zcat file | grep "stuff" -r shows what you want, you can do this for multiple files:

for name in *.gz ; do zcat "$name" | grep -a "stuff" | sed -e "s/^/${name}: /"  ; done

This command uses globbing (*) to expand to a list of .gz files in your working directory, then calls zcat for extraction, grep for the search and sed for prefixing with the filename on each of the files.

Note that if you are working with gzipped tarballs, most people give them a .tgz or .tar.gz instead of just .gz extension.

0

This will output nameOfFileInTar:LineNumber:Match. Invoke with greptar.sh tarfile.tar pattern

If you don't want the line number, remove the -n option. If you only want the line number, add |cut -f1 -d: after the grep

#!/bin/bash

TARFILE=$1
PATTERN=$2
tar ztf $TARFILE | while read -r FILE
do
  res=$(tar zxf $TARFILE $FILE  -O | grep -n $2 )
    if [[ $? == 0 ]]; then
      echo "$res" | while read -r line; do
        echo $FILE:$line;
      done
    fi
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.