#!/bin/rc # ogrep: "grep -o" # -S: use sam which supports \n in regexp but is slower rfork e fn usage{ echo 'usage: ogrep [ -S ] pattern [ file ... ]' >[1=2] exit usage } sam=0 while(~ $1 -*){ switch($1){ case -S; sam=1 case *; usage } shift } if(~ $#* 0) usage pat=$1 shift if(~ $sam 1){ ssam ' y/'$pat'/ c/\n/ 1d' $* exit } pat=`{echo $pat | sed 's/\\/\\\\/g'} awk ' BEGIN { pat = "'$"pat'" } { s = $0 while((p = match(s, pat)) != 0) { print substr(s, p, RLENGTH) s = substr(s, p + RLENGTH) } } ' $*