2025-02-24 04:42:45 +01:00
|
|
|
#! /bin/sh
|
|
|
|
# Zgrep - Grep wrapper for compressed files.
|
|
|
|
# Copyright (C) 2008, 2009 Antonio Diaz Diaz.
|
|
|
|
#
|
|
|
|
# This script is free software: you have unlimited permission
|
|
|
|
# to copy, distribute and modify it.
|
|
|
|
|
|
|
|
LC_ALL=C
|
|
|
|
export LC_ALL
|
|
|
|
args=
|
|
|
|
have_pat=0
|
|
|
|
list=0
|
|
|
|
no_name=0
|
2025-02-24 04:44:01 +01:00
|
|
|
two_hyphens=0
|
2025-02-24 04:42:45 +01:00
|
|
|
|
|
|
|
# Loop over args until pattern is found
|
|
|
|
while [ x"$1" != x ] ; do
|
|
|
|
|
|
|
|
case "$1" in
|
|
|
|
--help | --he* | -h)
|
|
|
|
echo "Zgrep - Grep wrapper for compressed files."
|
|
|
|
echo
|
|
|
|
echo "Zgrep is a wrapper script around the grep command that allows"
|
|
|
|
echo "transparent search on any combination of compressed and non-compressed"
|
|
|
|
echo "files. If any given file is compressed, its uncompressed content is"
|
|
|
|
echo "used. If a given file does not exist, zgrep tries the compressed file"
|
2025-02-24 04:57:21 +01:00
|
|
|
echo "names corresponding to the supported compressors. If no files are"
|
2025-02-24 04:57:56 +01:00
|
|
|
echo "specified, data is read from standard input, decompressed if needed, and"
|
|
|
|
echo "fed to grep. Data read from standard input must be of the same type; all"
|
|
|
|
echo "uncompressed or all compressed with the same compressor."
|
2025-02-24 04:44:01 +01:00
|
|
|
echo "The supported compressors are gzip, bzip2, lzip and xz."
|
2025-02-24 04:42:45 +01:00
|
|
|
echo
|
2025-02-24 04:51:33 +01:00
|
|
|
echo "Zegrep is a shortcut for \"zgrep -E\""
|
|
|
|
echo "Zfgrep is a shortcut for \"zgrep -F\""
|
|
|
|
echo
|
2025-02-24 04:44:01 +01:00
|
|
|
echo "Usage: $0 [OPTIONS] [GREP_OPTIONS] PATTERN [FILES]"
|
2025-02-24 04:42:45 +01:00
|
|
|
echo
|
|
|
|
echo "GREP_OPTIONS are passed directly to grep."
|
|
|
|
echo "The exit status from grep is preserved."
|
|
|
|
echo
|
|
|
|
echo "Options:"
|
|
|
|
echo " -h, --help display this help and exit"
|
|
|
|
echo " -V, --version output version information and exit"
|
|
|
|
echo
|
2025-02-24 04:44:01 +01:00
|
|
|
echo "Report bugs to zutils-bug@nongnu.org"
|
|
|
|
echo "Zutils home page: http://www.nongnu.org/zutils/zutils.html"
|
2025-02-24 04:42:45 +01:00
|
|
|
exit 0 ;;
|
|
|
|
--version | --ve* | -V)
|
|
|
|
echo "Zgrep VERSION"
|
|
|
|
echo "Copyright (C) 2009 Antonio Diaz Diaz."
|
|
|
|
echo "This script is free software: you have unlimited permission"
|
|
|
|
echo "to copy, distribute and modify it."
|
|
|
|
exit 0 ;;
|
|
|
|
-[drRzZ] | --di* | --exc* | --inc* | --nu* | --rec*)
|
2025-02-24 04:44:01 +01:00
|
|
|
echo "$0: option $1 not supported"
|
2025-02-24 04:42:45 +01:00
|
|
|
exit 1 ;;
|
|
|
|
-e?* | -f?* | --file=* | --reg*=*)
|
|
|
|
args="${args} $1"; have_pat=1 ;;
|
|
|
|
-e | -f | --file | --reg*)
|
|
|
|
args="${args} $1 $2"; shift; have_pat=1 ;;
|
|
|
|
--*=*)
|
|
|
|
args="${args} $1" ;;
|
|
|
|
-[ABCDm] | --af* | --be* | --binary-* | --con* | --de* | --[lm]a*)
|
|
|
|
args="${args} $1 $2"; shift ;;
|
|
|
|
-l | --files-with-*)
|
|
|
|
args="${args} $1"; list=1 ;;
|
|
|
|
-h | --no-f*)
|
|
|
|
args="${args} $1"; no_name=1 ;;
|
|
|
|
--)
|
2025-02-24 04:44:01 +01:00
|
|
|
shift ; two_hyphens=1 ; break ;;
|
2025-02-24 04:42:45 +01:00
|
|
|
-?*)
|
|
|
|
args="${args} $1" ;;
|
|
|
|
*)
|
2025-02-24 04:44:01 +01:00
|
|
|
if [ ${have_pat} = 0 ]; then args="${args} $1"; have_pat=1
|
2025-02-24 04:42:45 +01:00
|
|
|
else break
|
|
|
|
fi ;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
2025-02-24 04:44:01 +01:00
|
|
|
if [ ${have_pat} = 0 ]; then
|
|
|
|
echo "$0: Pattern not found; use --help for usage." 1>&2
|
2025-02-24 04:42:45 +01:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2025-02-24 04:57:21 +01:00
|
|
|
if [ $# = 0 ]; then
|
2025-02-24 04:57:56 +01:00
|
|
|
bindir=`echo "$0" | sed -e 's,[^/]*$,,'`
|
|
|
|
prog_name=`"${bindir}"zutils -t`
|
|
|
|
case "${prog_name}" in
|
|
|
|
gzip) prog="gzip -cdfq" ;;
|
|
|
|
bzip2) prog="bzip2 -cdfq" ;;
|
|
|
|
lzip) prog="lzip -cdfq" ;;
|
|
|
|
xz) prog="xz -cdfq" ;;
|
|
|
|
*) prog=cat ;;
|
|
|
|
esac
|
|
|
|
{ "${bindir}"zutils -m ${prog_name} ; cat ; } | ${prog} | grep ${args}
|
2025-02-24 04:57:21 +01:00
|
|
|
exit $?
|
|
|
|
fi
|
|
|
|
|
2025-02-24 04:42:45 +01:00
|
|
|
retval=0
|
|
|
|
for i in "$@" ; do
|
2025-02-24 04:44:01 +01:00
|
|
|
if [ "$i" = "--" ] && [ ${two_hyphens} = 0 ] ; then two_hyphens=1
|
2025-02-24 04:42:45 +01:00
|
|
|
else
|
2025-02-24 04:51:33 +01:00
|
|
|
if [ ! -f "$i" ]; then
|
|
|
|
if [ -f "$i.gz" ]; then i="$i.gz"
|
|
|
|
elif [ -f "$i.bz2" ]; then i="$i.bz2"
|
|
|
|
elif [ -f "$i.lz" ]; then i="$i.lz"
|
|
|
|
elif [ -f "$i.xz" ]; then i="$i.xz"
|
|
|
|
else
|
|
|
|
echo "$0: File \"$i\" not found or not a regular file" 1>&2
|
|
|
|
if [ ${retval} = 0 ]; then retval=1 ; fi
|
|
|
|
continue
|
|
|
|
fi
|
2025-02-24 04:44:01 +01:00
|
|
|
fi
|
2025-02-24 04:51:33 +01:00
|
|
|
bindir=`echo "$0" | sed -e 's,[^/]*$,,'`
|
|
|
|
prog_name=`"${bindir}"zutils -t -- "$i"`
|
|
|
|
case "${prog_name}" in
|
|
|
|
gzip) prog="gzip -cdfq" ;;
|
|
|
|
bzip2) prog="bzip2 -cdfq" ;;
|
|
|
|
lzip) prog="lzip -cdfq" ;;
|
|
|
|
xz) prog="xz -cdfq" ;;
|
|
|
|
*) prog=cat ;;
|
|
|
|
esac
|
2025-02-24 04:44:01 +01:00
|
|
|
if [ ${list} = 1 ]; then
|
|
|
|
${prog} -- "$i" | grep ${args} 2>&1 > /dev/null && echo "$i"
|
|
|
|
r=$?
|
|
|
|
elif [ $# = 1 ] || [ ${no_name} = 1 ]; then
|
|
|
|
${prog} -- "$i" | grep ${args}
|
|
|
|
r=$?
|
|
|
|
else
|
2025-02-24 04:51:33 +01:00
|
|
|
j=`printf "%s" "$i" | sed -e 's,\\\\,\\\\\\\\,g'`
|
|
|
|
j=`printf "%s" "$j" | sed -e 's,|,\\\\|,g'`
|
|
|
|
j=`printf "%s" "$j" | sed -e 's,&,\\\\&,g'`
|
2025-02-24 04:44:01 +01:00
|
|
|
j=`printf "%s" "$j" | tr '\n' ' '`
|
2025-02-24 04:51:33 +01:00
|
|
|
${prog} -- "$i" | grep ${args} | sed -e "s,^,${j}:,"
|
2025-02-24 04:44:01 +01:00
|
|
|
r=$?
|
|
|
|
fi
|
|
|
|
[ $r != 0 ] && retval="$r"
|
2025-02-24 04:42:45 +01:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
exit ${retval}
|