Performing grep operation in tar files without extracting

后端 未结 6 1367
梦谈多话
梦谈多话 2020-12-05 00:28

I have list of files which contain particular patterns, but those files have been tarred. Now I want to search for the pattern in the tar file, and to know which files conta

6条回答
  •  离开以前
    2020-12-05 01:01

    the tar command has a -O switch to extract your files to standard output. So you can pipe those output to grep/awk

    tar xvf  test.tar -O | awk '/pattern/{print}'
    
    tar xvf  test.tar -O | grep "pattern"
    

    eg to return file name one pattern found

    tar tf myarchive.tar | while read -r FILE
    do
        if tar xf test.tar $FILE  -O | grep "pattern" ;then
            echo "found pattern in : $FILE"
        fi
    done
    

提交回复
热议问题