-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathfindIn
More file actions
executable file
·75 lines (66 loc) · 1.56 KB
/
findIn
File metadata and controls
executable file
·75 lines (66 loc) · 1.56 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
#
# Whenever I want to find something in a folder hierarchy,
# but only for a specific type of files...
#
# e.g.:
#
# findIn .py '^def.*order'
#
# or
#
# findIn '.[ch]' '^void'
#
# To make a case-insensitive search, pass "-i" in front (before the extension)
# To make a whole-word search, pass "-w" in front (before the extension)
usage() {
echo -e "Usage: $0 [-h] [-i] [-w] .extension regexp"
echo -e "Where:"
echo -e "\t-h\t\tshow this help"
echo -e "\t-w\t\tperform word-boundary search"
echo -e "\t-i\t\tperform case insensitive search"
echo -e "\t-n\t\tshow line numbers in results"
echo -e "\t.extension\tthe file extension to search in (e.g. .c, .py, etc)"
echo -e "\tregexp\t\tthe regular expression to search for"
exit 1
}
[ $# -eq 0 ] && usage
CASE=""
WORD=""
LINENOS=""
while getopts "iwhn" o ; do
case "${o}" in
h)
usage
;;
i)
CASE="-i"
;;
w)
WORD="-w"
;;
n)
LINENOS="-n"
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
EXTENSION="$(echo "$1" | sed 's/\./\\./g')"
shift
# If output goes to a tty, I don't want a wrap around, but I do want colors
if [ -t 1 ] ; then
COLORS="always"
tput rmam # Stop wrapping
else
COLORS="none"
fi
# Go fetch!
find . -type f -iname '*'"${EXTENSION}" -exec \
grep ${CASE} ${WORD} ${LINENOS} --color="$COLORS" -- "$@" '{}' /dev/null \; 2>/dev/null
# Reset wrapping
if [ -t 1 ] ; then
tput smam
fi