zgrep revision 1.5
11.1Swiz#!/bin/sh 21.2Swiz# 31.5Syamt# $NetBSD: zgrep,v 1.5 2006/05/03 16:48:29 yamt 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.5Syamthyphen=0 361.1Swiz 371.1Swizprg=$0 381.1Swiz 391.1Swiz# handle being called 'zegrep' or 'zfgrep' 401.1Swizcase ${prg} in 411.1Swiz *zegrep) 421.1Swiz grep_args="-E";; 431.1Swiz *zfgrep) 441.1Swiz grep_args="-F";; 451.1Swizesac 461.1Swiz 471.1Swiz# skip all options and pass them on to grep taking care of options 481.1Swiz# with arguments, and if -e was supplied 491.1Swiz 501.1Swizwhile [ $# -gt 0 -a ${endofopts} -eq 0 ] 511.1Swizdo 521.1Swiz case $1 in 531.1Swiz # from GNU grep-2.5.1 -- keep in sync! 541.1Swiz -[ABCDXdefm]) 551.5Syamt if [ $# -lt 2 ] 561.5Syamt then 571.5Syamt echo "${prg}: missing argument for $1 flag" >&2 581.5Syamt exit 1 591.5Syamt fi 601.1Swiz case $1 in 611.1Swiz -e) 621.5Syamt pattern="$2" 631.5Syamt pattern_found=1 641.5Syamt shift 2 651.5Syamt break 661.5Syamt ;; 671.1Swiz *) 681.1Swiz ;; 691.1Swiz esac 701.1Swiz grep_args="${grep_args} $1 $2" 711.1Swiz shift 2 721.1Swiz ;; 731.1Swiz --) 741.1Swiz shift 751.1Swiz endofopts=1 761.1Swiz ;; 771.1Swiz -) 781.5Syamt hyphen=1 791.5Syamt shift 801.1Swiz ;; 811.1Swiz -*) 821.1Swiz grep_args="${grep_args} $1" 831.1Swiz shift 841.1Swiz ;; 851.1Swiz *) 861.1Swiz # pattern to grep for 871.1Swiz endofopts=1 881.1Swiz ;; 891.1Swiz esac 901.1Swizdone 911.1Swiz 921.1Swiz# if no -e option was found, take next argument as grep-pattern 931.1Swizif [ ${pattern_found} -lt 1 ] 941.1Swizthen 951.5Syamt if [ $# -ge 1 ]; then 961.5Syamt pattern="$1" 971.5Syamt shift 981.5Syamt elif [ ${hyphen} -gt 0 ]; then 991.5Syamt pattern="-" 1001.5Syamt else 1011.1Swiz echo "${prg}: missing pattern" >&2 1021.1Swiz exit 1 1031.1Swiz fi 1041.1Swizfi 1051.1Swiz 1061.1Swiz# call grep ... 1071.1Swizif [ $# -lt 1 ] 1081.1Swizthen 1091.1Swiz # ... on stdin 1101.5Syamt ${zcat} -fq - | ${grep} ${grep_args} -- "${pattern}" - 1111.1Swizelse 1121.1Swiz # ... on all files given on the command line 1131.1Swiz while [ $# -gt 0 ] 1141.1Swiz do 1151.5Syamt ${zcat} -fq -- "$1" | ${grep} -H --label="${1}" ${grep_args} -- "${pattern}" - 1161.1Swiz shift 1171.1Swiz done 1181.1Swizfi 1191.1Swiz 1201.1Swizexit 0 121