Monday, 20 February 2012

Script to grep a word inside a tar file

search="123"
tar -tf one.tar | while read filename
do
        if [ -f "${filename}" ]
        then
                mv "${filename}" "${filename}.sav"
        fi
        #
        tar -xf one.tar "${filename}"
        #
        found=`grep -l "${search}" "${filename}"`
        if [ ! "${found}""X" = "X" ]
        then
                echo "Found \"${search}\" in \"${found}\""
                grep "${search}" "${filename}"
                echo ""
        fi
        #
        if [ -f "${filename}.sav" ]
        then
                mv "${filename}.sav" "${filename}"
        fi
done

No comments: