-
Notifications
You must be signed in to change notification settings - Fork 11
/
grepr.sh
196 lines (181 loc) · 4.75 KB
/
grepr.sh
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/bin/ksh
#############################################################
#
# Looking for a file with a certain pattern? But you can't
# remember where the file is, but know its somewhere in or
# below the current directory. This is for you.
#
# You do not give filenames as the setup is to look at all
# files in the cwd, or look in cwd and all subdirectories.
#
# If you want only to look at files in the current directory
# and not any subdirectories DON'T use the -R option.
# DEFAULT is to do only files in the current directory.
#
# Should be able to use all grep capabilities.
#
# SYNTAX:
# grepr [-R] [arguments_for_grep] pattern
#
# The -R tells it to go recursively, else it will
# look only at files in the current directory.
#
# EXAMPLES:
# grepr -R -b pattern
# grepr -R pattern
# grepr -v pattern
# grepr -vb pattern
#
# OTHER POSSIBILITIES:
# EXAMPLES from the command line:
#
# ### Won't do binary and other data type files, just text.
# alias grepr 'grep \!* `find . -type f -print`'
#
# ### To do binary and other data type files
# ### but does have problems with control characters
# ### Maybe pipe to 'tr -d '\xx''. (Limited help.)
# alias egrepr 'egrep \!* `find . -type f -print`'
#
# LIMITATION:
# Does not look at ALL non-text files (ONLY looks at
# FrameMaker, PDF, Microsoft, and compiled C code data
# formats (binary), graphics, and compressed files).
#
# Does not look at 'dot' files (.login for example) as
# only an 'ls' is done.
#
# BUGS:
# It does runs VERY slowly - intensive for the filesystem.
#
# Written by: Matt Baker
# Created on: 17 Jun 93
# Last edited on: 27 Aug 2008
# Version 3.3
#
# Tested on following:
# CPU: Sun 3, Sparc, DECstation 5000, Alpha and many others
# OS: SunOS 4.1.1, Ultrix 4.1, OSF 3.2, Solaris 2.X and many others
#
#############################################################
###################
# Check to see if at least one command line argument (pattern)
###################
if [ $# -lt 1 ]
then
echo "Usage: $0 [-R] {grep_options} pattern"
exit 1
fi
###################
# set recursion var and shift so its
# not part of the options (resulting
# in incorrect grep option)
###################
if [ "$1" = "-R" ]
then
RECURSION="$1"
shift
else
RECURSION=""
fi
###################
# Make sure no filenames are given
###################
#for OPT in "$@"
#do
#if [ -f "$OPT" -o -d "$OPT" ]
#then
#echo "*** ERROR: No filenames are allowed ***"
#echo "Usage: $0 [-R] {grep_options} pattern"
#exit 1
#fi
#done
#########################
# Declare function 'lookup'
#########################
###################
# ORDER OF EVENTS
# Get current pwd ls of files
# IF: [directory] and -R => call function and repeat (recursive)
# ELSE IF: if [binary,data,framemaker,graphics] => strings | grep
# ELSE IF: if [compress] => zcat | grep
# ELSE: (plain file) grep
###################
lookup () {
# get rid of . and .. from list
for FILE in $(ls -a | egrep -wv '\.|\.\.')
do
################
# for data/binary test
################
FILE_TYPE=$(file "$FILE" | awk -F: '{print $2}')
################
# if directory, recursion, if not set
################
if [ -d $FILE -a -n "$RECURSION" ]
then
cd $FILE > /dev/null 2>&1
lookup "$@"
cd ..
################
# FrameMaker, SunOS compiled code, graphics, OSF compiled code
# added PDF, Microsoft
################
elif [[ -n $(echo $FILE_TYPE | egrep -we \
'executable|Frame|Microsoft|PDF|rasterfile|data|demand' ) ]]
then
OUTPUT="$(strings "$FILE" | grep "$@")"
if [[ -n $OUTPUT ]]
then
echo "$(pwd)/$FILE:"
echo $OUTPUT
echo ""
fi
################
# compressed
################
elif [ "$(echo $FILE_TYPE | awk '{print $2}')" = "compressed" ]
then
OUTPUT="$(zcat $FILE | grep "$@")"
if [[ -n $OUTPUT ]]
then
echo "$(pwd)/$FILE:"
echo $OUTPUT
echo ""
fi
################
# normal text file
################
else
OUTPUT="$(grep "$@" $FILE)"
if [[ -n $OUTPUT ]]
then
echo "$(pwd)/$FILE:"
echo $OUTPUT
echo ""
fi
fi
done
}
#####################
# Call function
#####################
lookup "$@"
#####################
# Remove temp file
#####################
if [ -s /tmp/$$ ]
then
rm /tmp/$$
fi
##############################################################################
### This script is submitted to BigAdmin by a user of the BigAdmin community.
### Sun Microsystems, Inc. is not responsible for the
### contents or the code enclosed.
###
###
### Copyright 2008 Sun Microsystems, Inc. ALL RIGHTS RESERVED
### Use of this software is authorized pursuant to the
### terms of the license found at
### http://www.sun.com/bigadmin/common/berkeley_license.html
##############################################################################