zgrep revision 1.2
11.1Swiz#!/bin/sh 21.2Swiz# 31.2Swiz# $NetBSD: zgrep,v 1.2 2003/12/28 17:26:48 wiz Exp $ 41.2Swiz# 51.1Swiz# Copyright (c) 2003 Thomas Klausner. 61.1Swiz# 71.1Swiz# Redistribution and use in source and binary forms, with or without 81.1Swiz# modification, are permitted provided that the following conditions 91.1Swiz# are met: 101.1Swiz# 1. Redistributions of source code must retain the above copyright 111.1Swiz# notice, this list of conditions and the following disclaimer. 121.1Swiz# 2. Redistributions in binary form must reproduce the above copyright 131.1Swiz# notice, this list of conditions and the following disclaimer in the 141.1Swiz# documentation and/or other materials provided with the distribution. 151.1Swiz# 3. The name of the author may not be used to endorse or promote products 161.1Swiz# derived from this software without specific prior written permission. 171.1Swiz# 181.1Swiz# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 191.1Swiz# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 201.1Swiz# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 211.1Swiz# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 221.1Swiz# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 231.1Swiz# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 241.1Swiz# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 251.1Swiz# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 261.1Swiz# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 271.1Swiz# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 281.1Swiz 291.1Swizgrep=/usr/bin/grep 301.1Swizzcat=/usr/bin/zcat 311.1Swiz 321.1Swizendofopts=0 331.1Swizpattern_found=0 341.1Swizgrep_args="" 351.1Swiz 361.1Swizprg=$0 371.1Swiz 381.1Swiz# handle being called 'zegrep' or 'zfgrep' 391.1Swizcase ${prg} in 401.1Swiz *zegrep) 411.1Swiz grep_args="-E";; 421.1Swiz *zfgrep) 431.1Swiz grep_args="-F";; 441.1Swizesac 451.1Swiz 461.1Swiz# skip all options and pass them on to grep taking care of options 471.1Swiz# with arguments, and if -e was supplied 481.1Swiz 491.1Swizwhile [ $# -gt 0 -a ${endofopts} -eq 0 ] 501.1Swizdo 511.1Swiz case $1 in 521.1Swiz # from GNU grep-2.5.1 -- keep in sync! 531.1Swiz -[ABCDXdefm]) 541.1Swiz case $1 in 551.1Swiz -e) 561.1Swiz pattern_found=1;; 571.1Swiz *) 581.1Swiz ;; 591.1Swiz esac 601.1Swiz 611.1Swiz if [ $# -lt 2 ] 621.1Swiz then 631.1Swiz echo "${prg}: missing argument for ${saved_arg} flag" >&2 641.1Swiz exit 1 651.1Swiz fi 661.1Swiz grep_args="${grep_args} $1 $2" 671.1Swiz shift 2 681.1Swiz ;; 691.1Swiz --) 701.1Swiz grep_args="${grep_args} $1" 711.1Swiz shift 721.1Swiz endofopts=1 731.1Swiz ;; 741.1Swiz -) 751.1Swiz endofopts=1 761.1Swiz ;; 771.1Swiz -*) 781.1Swiz grep_args="${grep_args} $1" 791.1Swiz shift 801.1Swiz ;; 811.1Swiz *) 821.1Swiz # pattern to grep for 831.1Swiz endofopts=1 841.1Swiz ;; 851.1Swiz esac 861.1Swizdone 871.1Swiz 881.1Swiz# if no -e option was found, take next argument as grep-pattern 891.1Swizif [ ${pattern_found} -lt 1 ] 901.1Swizthen 911.1Swiz if [ $# -lt 1 ] 921.1Swiz then 931.1Swiz echo "${prg}: missing pattern" >&2 941.1Swiz exit 1 951.1Swiz fi 961.1Swiz pattern=$1 971.1Swiz shift 981.1Swizfi 991.1Swiz 1001.1Swiz# call grep ... 1011.1Swizif [ $# -lt 1 ] 1021.1Swizthen 1031.1Swiz # ... on stdin 1041.1Swiz ${zcat} - | ${grep} ${grep_args} ${pattern} - 1051.1Swizelse 1061.1Swiz # ... on all files given on the command line 1071.1Swiz while [ $# -gt 0 ] 1081.1Swiz do 1091.1Swiz ${zcat} -- "$1" | ${grep} -H --label="${1}" ${pattern} ${grep_args} - 1101.1Swiz shift 1111.1Swiz done 1121.1Swizfi 1131.1Swiz 1141.1Swizexit 0 115