~charles/dotfiles

61f2e44ed1c7059f7b48c75f28c4a27c79ce453e — Charles Daniels 3 years ago 0e70576
add file patterns to gr
1 files changed, 13 insertions(+), 2 deletions(-)

M overlay/bin/gr
M overlay/bin/gr => overlay/bin/gr +13 -2
@@ 1,10 1,21 @@
#!/bin/sh

find . -type f -print | while read -r f ; do
if [ $# -eq 1 ] ; then
	pattern="$1"
	file_pattern=".*"
elif [ $# -eq 2 ] ; then
	pattern="$1"
	file_pattern="$2"
else
	echo "$(basename "$0") REGEX [FILE PATTERN]" 1>&2
	exit 1
fi

find . -type f -print | perl -ne 'print if /'"$file_pattern"'/' | while read -r f ; do
	if [ ! -e "$f" ] ; then continue ; fi

	hdr="$f"
	grep -U -i -n "$@" "$f" | while read -r line ; do
	grep -U -i -n "$pattern" "$f" | while read -r line ; do
		if [ ! -z "$hdr" ] ; then
			printf "%s\n" "$hdr"
			hdr=""