Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 13 Sep 2002 03:29:20 -0700
From:      Mike Makonnen <mtm@identd.net>
To:        Akinori MUSHA <knu@iDaemons.org>
Cc:        kris@obsecurity.org, sobomax@FreeBSD.ORG, obrien@FreeBSD.ORG, cvs-committers@FreeBSD.ORG, cvs-all@FreeBSD.ORG
Subject:   Re: cvs commit: src/etc newsyslog.conf
Message-ID:  <20020913032920.0cbd1a4b.mtm@identd.net>
In-Reply-To: <86ofb3c75p.wl@archon.local.idaemons.org>
References:  <200209121728.g8CHS7An013425@freefall.freebsd.org> <3D80D22F.B7BDF10E@FreeBSD.org> <20020912193532.GA21745@xor.obsecurity.org> <86ofb3c75p.wl@archon.local.idaemons.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On Fri, 13 Sep 2002 04:57:54 +0900
"Akinori MUSHA" <knu@iDaemons.org> wrote:

> 
> I'd note that our zgrep(1) does not support bz2 files nor have we
> something called bzgrep(1).  It is a mess that you can't do a grep
> over uncompressed files, gzip'd files and bzip2'd files at once.
> 
> Do we have as handy a command as the following one that used to work
> before the switchover?
> 
> $ zgrep foo messages*

Well,

The current zgrep(1) is really grep(1); however, there is a zgrep in
src/gnu/usr.bin/gzip, which with th following patch will support bziped files.
Just install _that_ script instead of hardlinking zgrep to grep at
install time, and then hardlink bzgrep to zgrep.

$ zgrep foo messages * 
will do what you want irrespective of compression type
(gzip or bzip2). But whether you use zgrep or bzgrep will determine how files
with unknown suffixes will be decompressed.

Cheers,
Mike Makonnen.

Index: gnu/usr.bin/gzip/zgrep
===================================================================
RCS file: /home/ncvs/src/gnu/usr.bin/gzip/zgrep,v
retrieving revision 1.3
diff -u -r1.3 zgrep
--- gnu/usr.bin/gzip/zgrep	27 Aug 1999 23:35:55 -0000	1.3
+++ gnu/usr.bin/gzip/zgrep	13 Sep 2002 10:14:09 -0000
@@ -5,8 +5,45 @@
 #
 # $FreeBSD: src/gnu/usr.bin/gzip/zgrep,v 1.3 1999/08/27 23:35:55 peter Exp $
 
+GZIP="/usr/bin/gzip"
+BZIP2="/usr/bin/bzip2"
+zipcmd="$GZIP"
+zipflags="-cdfq"	# gzip and bzip2 use (mostly) the same flags
+defaultzip="gzip"
+
+# cmd_from_suffix file
+#	Takes one optional argument (file). Sets the global zipcmd and
+#	zipflag variables according to the type of compressed file. If
+#	a file is not specified, it uses the default compression type.
+#
+cmd_from_suffix()
+{
+  _match=1
+  if test $# -ne 0 ; then
+    if test "`expr "$1" : '.*\.\(gz\)$'`" = "gz" ; then
+      zipcmd="$GZIP"
+    elif test "`expr "$1" : '.*\.\(bz2\)$'`" = "bz2" ; then
+      zipcmd="$BZIP2"
+    else
+      _match=0
+    fi
+  fi
+  if test $# -eq 0 -o $_match -eq 0 ; then 
+    if test "$defaultzip" = "bzip2" ; then
+      zipcmd="$BZIP2"
+    else
+      zipcmd="$GZIP"
+    fi
+  fi
+  zipcmd="$zipcmd $zipflags"
+}
+
 prog=`echo $0 | sed 's|.*/||'`
 case "$prog" in
+	bz*) defaultzip="bzip2"		;;
+	z*)  defaultzip="gzip"		;;
+esac
+case "$prog" in
 	*egrep)	grep=${EGREP-egrep}	;;
 	*fgrep)	grep=${FGREP-fgrep}	;;
 	*)	grep=${GREP-grep}	;;
@@ -49,22 +86,25 @@
   *h*) silent=1
 esac
 
+zipcmd="$zipcmd $zipflags"
 if test $fileno -eq 0; then
-  gzip -cdfq | $grep $opt "$pat"
+  cmd_from_suffix
+  $zipcmd | $grep $opt "$pat"
   exit $?
 fi
 eval set "$A" # files in $1, $2 ...
 
 res=0
 for i do
+  cmd_from_suffix $i
   if test $list -eq 1; then
-    gzip -cdfq "$i" | $grep $opt "$pat" > /dev/null && echo $i
+    $zipcmd "$i" | $grep $opt "$pat" > /dev/null && echo $i
     r=$?
   elif test $# -eq 1 -o $silent -eq 1; then
-    gzip -cdfq "$i" | $grep $opt "$pat"
+    $zipcmd "$i" | $grep $opt "$pat"
     r=$?
   else
-    gzip -cdfq "$i" | $grep $opt "$pat" | sed "s|^|${i}:|"
+    $zipcmd "$i" | $grep $opt "$pat" | sed "s|^(standard input):|${i}:|"
     r=$?
   fi
   test "$r" -ne 0 && res="$r"

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe cvs-all" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020913032920.0cbd1a4b.mtm>