~charles/dotfiles

ref: f3d8c43ffc40a3ddf482a222c3399052aa6b2f4b dotfiles/overlay/bin/gr -rwxr-xr-x 492 bytes
f3d8c43f — Charles Daniels gr ignore un-readable files 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/sh

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 [ ! -r "$f" ] ; then continue ; fi

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

done