From 21be8e78d7a629f57f28620dc4445bc230818ef4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Tue, 15 Jun 2021 07:46:56 +0200 Subject: Improve 'g' utility --- g | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/g b/g index 04cbc36..0a22892 100755 --- a/g +++ b/g @@ -1,5 +1,28 @@ #!/bin/sh -p grep -n "$*" $( - find . -name '*.c' -or -name '*.h' -or -name '*.cc' | - sed s,./,,) +IFS=' +' + +pat= +opt= +i=0 + +while [ $((i++)) -lt $# ]; do + case "$1" in + -*) opt=`printf '%s%s\n' "$opt" $1` + shift ;; + *) break ;; + esac +done + +[ $# -eq 0 ] && { echo usage: $0 [-...] pattern [file ...] 1>&2; exit 1; } + +pat=$1 +shift + +files=`find . -name '*.c' -or -name '*.h' -or -name '*.cc' | sed s,./,,` +[ -z "$files" ] && [ -z "$*" ] && { echo no matching files found 1>&2; exit 1; } + +# todo (?): treat argument \*.x like -or -name \*.x + +p grep $opt -n "$pat" $files "$@" -- cgit v1.2.3