14887Schin######################################################################## 24887Schin# # 34887Schin# This software is part of the ast package # 4*12068SRoger.Faulkner@Oracle.COM# Copyright (c) 1982-2010 AT&T Intellectual Property # 54887Schin# and is licensed under the # 64887Schin# Common Public License, Version 1.0 # 78462SApril.Chin@Sun.COM# by AT&T Intellectual Property # 84887Schin# # 94887Schin# A copy of the License is available at # 104887Schin# http://www.opensource.org/licenses/cpl1.0.txt # 114887Schin# (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) # 124887Schin# # 134887Schin# Information and Software Systems Research # 144887Schin# AT&T Research # 154887Schin# Florham Park NJ # 164887Schin# # 174887Schin# David Korn <dgk@research.att.com> # 184887Schin# # 194887Schin######################################################################## 204887Schinfunction err_exit 214887Schin{ 224887Schin print -u2 -n "\t" 234887Schin print -u2 -r ${Command}[$1]: "${@:2}" 244887Schin let Errors+=1 254887Schin} 264887Schinalias err_exit='err_exit $LINENO' 274887Schin 284887SchinCommand=${0##*/} 294887Schininteger Errors=0 304887Schin 3110898Sroland.mainz@nrubsig.orgtmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; } 3210898Sroland.mainz@nrubsig.orgtrap "cd /; rm -rf $tmp" EXIT 3310898Sroland.mainz@nrubsig.org 344887Schinfunction grep 354887Schin{ 364887Schin # 374887Schin # SHELL VERSION OF GREP 384887Schin # 394887Schin vflag= xflag= cflag= lflag= nflag= 404887Schin set -f 414887Schin while ((1)) # look for grep options 424887Schin do case "$1" in 434887Schin -v*) vflag=1;; 444887Schin -x*) xflag=1;; 454887Schin -c*) cflag=1;; 464887Schin -l*) lflag=1;; 474887Schin -n*) nflag=1;; 484887Schin -b*) print 'b option not supported';; 494887Schin -e*) shift;expr="$1";; 504887Schin -f*) shift;expr=$(< $1);; 514887Schin -*) print $0: 'unknown flag';return 2;; 524887Schin *) 534887Schin if test "$expr" = '' 544887Schin then expr="$1";shift 554887Schin fi 564887Schin test "$xflag" || expr="*${expr}*" 574887Schin break;; 584887Schin esac 594887Schin shift # next argument 604887Schin done 614887Schin noprint=$vflag$cflag$lflag # don't print if these flags are set 624887Schin integer n=0 c=0 tc=0 nargs=$# # initialize counters 634887Schin for i in "$@" # go thru the files 644887Schin do if ((nargs<=1)) 654887Schin then fname='' 664887Schin else fname="$i": 674887Schin fi 684887Schin test "$i" && exec 0< $i # open file if necessary 694887Schin while read -r line # read in a line 704887Schin do let n=n+1 714887Schin case "$line" in 724887Schin $expr) # line matches pattern 734887Schin test "$noprint" || print -r -- "$fname${nflag:+$n:}$line" 744887Schin let c=c+1 ;; 754887Schin *) # not a match 764887Schin if test "$vflag" 774887Schin then print -r -- "$fname${nflag:+$n:}$line" 784887Schin fi;; 794887Schin esac 804887Schin done 814887Schin if test "$lflag" && ((c)) 824887Schin then print -r -- "$i" 834887Schin fi 844887Schin let tc=tc+c n=0 c=0 854887Schin done 864887Schin test "$cflag" && print $tc # print count if cflag is set 874887Schin let tc # set the return value 884887Schin} 894887Schin 9010898Sroland.mainz@nrubsig.orgcat > $tmp/grep <<\! 914887Schinthis is a food bar test 924887Schinto see how many lines find both foo and bar. 934887SchinSome line contain foo only, 944887Schinand some lines contain bar only. 954887SchinHowever, many lines contain both foo and also bar. 964887SchinA line containing foobar should also be counted. 974887SchinThere should be six lines with foo and bar. 984887SchinThere are only two line with out foo but with bar. 994887Schin! 1004887Schin 10110898Sroland.mainz@nrubsig.orgif (( $(grep -c 'foo*bar' $tmp/grep ) != 6)) 1024887Schinthen err_exit 1034887Schinfi 1044887Schinexit $((Errors)) 105