-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdfsearch
executable file
·420 lines (390 loc) · 11.4 KB
/
pdfsearch
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/bin/bash
version=0.1.2
############################################################################
## ##
## pdfsearch: A shell-script to search pdf files using regular ##
## expressions ##
## ##
## Author: Pavel Loskot, [email protected] ##
## ##
## Usage: "textblocks -help" ##
## ##
## License: GNU/GPL version 2 or later. No other warranty. ##
## ##
## Configuration file may appear in future versions. ##
## ##
## Developed and tested with GNU bash version 5.2.15 ##
## ##
############################################################################
trap "echo :exiting;exit 1" SIGINT SIGTERM
set -e
export LC_CTYPE="en_US.utf8"
this="${0/#*\/}"
PDFVIEW=/usr/bin/okular
TXTVIEW="/usr/bin/emacs --geometry 80x44+1020+0"
PDFTOTEXT="/usr/bin/pdftotext -eol unix -q"
if [[ "$#" == "0" || "$1" == @(-h|--help) ]]; then
echo "
$this - a shell script to find the frequency of occrrence statistics of
complex regular expressions within a group of pdf files
Usage: pdfsearch [params] "pattern" files|-f <filename> [filters]
Patterns:
"p1\&p2\&p3" must contain all sub-patterns p1, p2, p3
"p1\|p2\|p3" must contain any sub-patterns p1, p2, p3
"@p1" pattern p1 passed directly to grep
"p1?n" must contain enough occurrences of p1 where
? can be >, <, >=, <=
"\(p1\|p2\)\&p3" combining patterns using parentheses
"@@p1p2p3" @@ raw pattern to be passed directly to grep
Commands:
-h, --help show this help and quit
-r, --refresh regenerate text files
-t, --textfiles search text files instead of pdf files
-s, --save <filename> save output to this file
-f, --file <filename> read input file names from this file
-q, --quiet do not display any messages
-g-<s1s2>, --grep-<s1s2> pass switches -s1 -s2 to grep
-n, --nomatch show results even if no match
-m, --multiline consider pattern may span over two consecutive lines
-a, --addup [\"?n\"] report sum of occurrences for multiple patterns
? can be >, <, >=, <=
Filters:
first sort, then select and possibly view
[R]SC, [R]SORTBYCOUNT [reverse] sort results by count
[R]SD, [R]SORTBYDATE [reverse] sort results by date substring
[R]SN, [R]SORTBYNAME [reverse] sort results by file name
BN, BASENAME only show basenames
FN, FULLNAME only show full names
CSV csv format
STAT show statistics of matches
HD, HEAD <n> only show first n records
TL, TAIL <n> only show last n records
GR, GREP /pat/ only show records matching pattern
VF, VIEWFILES view files
"
exit 0
fi
# defaults
quiet=no
refresh=no
pdffiles=yes
save=no
sfname=
fromfile=no
fname=
rawpattern=
files=
grepopt=-no
nomatch=no
addup=no
aucmp=">0"
multiline=no
filteropt=
args=
while [ $# -gt 0 ]; do
if [[ "$1" =~ .*\ .* ]]; then
args="$args:\"$1\""
shift; continue
fi
if [[ "$1" =~ ^-[a-z][a-z]+$ ]]; then
args="$args$(echo "${1/-/}" | sed 's/./:-&/g')"
shift; continue
fi
args="$args:$1"
shift
done
args="$args:"
while [[ ! -z "${args//:/}" ]]; do
case ${args%%:*} in
-q|--quiet)
quiet=yes
;;
-r|--refresh)
refresh=yes
;;
-t|--textfiles)
pdffiles=no
;;
-s|--save)
args=${args#*:}
save=yes
sfname=${args%%:*}
;;
-f|--file)
fromfile=yes
args=${args#*:}
fname=${args%%:*}
;;
-g-*|--grep-*)
grepopt="$grepopt $(echo "${args%%:*}"|sed 's/^-.*-//;s/./-& /g')"
;;
-n|--nomatch)
nomatch=yes
;;
-m|--multiline)
multiline=yes
;;
-a|--add)
addup=yes
args=${args#*:}
if [[ "$args" =~ ^[\<\>=]{1,2}[0-9]+ ]]; then
aucmp="${args%%:*}"
else
continue
fi
;;
-*)
echo unknown parameter "${args%%:*}"
exit 1
;;
*)
if [[ -z "$rawpattern" ]]; then
rawpattern=${args%%:*}
args=${args#*:}
continue
fi
break
esac
args=${args#*:}
done
rawpattern=${rawpattern//\"/}
args=${args//:/ }
re="\<[A-Z][A-Z][A-Z]*\>"
if [[ "$args" =~ $re ]]; then
n=$(echo "$args" | grep -boE "[A-Z][A-Z][A-Z\ ]" | sed -n '1s/:.*//p')
filteropt=${args:$n}
files=${args::$n}
else
files="$args"
fi
if [[ "$files" =~ ^\ *$ ]]; then
echo no files given, consider -t switch
exit 1
fi
if [[ "$pdffiles" == "yes" ]]; then
files=$(find $files | grep -i ".pdf")
s="$?"
else
files=$(find $files | grep -i ".txt")
s="$?"
fi
if [ "$s" -eq 1 ]; then
echo no pdf/txt files found
exit 1
fi
if [[ "$quiet" == "yes" ]] && [[ "$filteropt" =~ ^\ *$ ]]; then
echo no output filter and quiet switch on
exit 1
fi
[[ "$fromfile" == "yes" ]] && files="$(cat $fname)$fromfile"
if [[ "$save" == "yes" ]]; then
rm -f $sfname
touch $sfname
fi
# pattern pre-process
if [[ "$rawpattern" =~ ^@@.* ]]; then
PATTERN=${rawpattern:2}
else
IFS='|&' read -ra PATTERN <<< "${rawpattern//[()]/}"
fi
NPAT=${#PATTERN[@]}
declare -a results
c=0
for f in $files; do
echo -e -n "\r$f "
if [[ ! -f "$f" ]] && [[ "$quiet" == "no" ]]; then
echo -e "\r$f does not exist, skipping "
continue
fi
f1=${f%.*}
ext=${f##*.}
ext=${ext,,}
if [[ ! "$ext" =~ pdf|PDF|txt|TXT ]] && [[ "$quiet" == "no" ]]; then
echo -e "\runknown extension in $f, skipping "
continue
fi
if [[ ! -f "$f1.txt" ]] || [[ "$refresh" == "yes" ]]; then
if [[ ! -f "$f1.pdf" ]]; then
echo -e "\r$f1.pdf does not exist, skipping "
continue
fi
rm -f "$f1.txt"
$PDFTOTEXT $f1.pdf "$f1.txt"
fi
# pattern evaluation
testpattern=$(echo $rawpattern | \
sed 's/(/ ( /g;s/)/ ) /g;s/|/ -o /g;s/&/ -a /g')
msg=""
for rpat in "${PATTERN[@]}"; do
if [[ "$rpat" =~ ^@.* ]]; then
pattern=${pat:1}
m=$(grep $grepopt "$pattern" "$f1.txt" | sed 's/:.*//')
[[ -z "$m" ]] && mc=0 || mc=$(echo "$m" | wc -l)
testpattern="$mc -gt 0"
else
pattern=${rpat%%[<=>]*}
noc=${rpat##*[<=>]}
cmp=""
[[ "$pattern" == "$noc" ]] && noc=0
[[ "$noc" == "=" ]] && noc=0
case "$rpat" in
*"<="*) cmp="-le" ;;
*">="*) cmp="-ge" ;;
*">"*) cmp="-gt" ;;
*"<"*) cmp="-lt" ;;
*"="*) cmp="-eq" ;;
*) cmp="-gt" ;;
esac
if [[ "$multiline" == "yes" ]]; then
# experimental
m=$(cat "$f1.txt" | sed -n '/./,$p' | \
sed '/^\s*$/N;/^\n$/D' | sed 's/^$/_x_/' | \
sed ':a;N;/_x_/!ba;s/\n/ /g' | sed 's/_x_/\n/' | \
grep $grepopt "$pattern" "$f1.txt" | sed 's/:.*//')
else
m=$(grep $grepopt "$pattern" "$f1.txt" | sed 's/:.*//')
fi
[[ -z "$m" ]] && mc=0 || mc=$(echo "$m" | wc -l)
rpat1=$(echo "$rpat" | sed 's/^[ ]*//;s/[ ]*$//')
testpattern=$(echo $testpattern | sed "s/$rpat1/$mc $cmp $noc/")
[ $mc $cmp $noc ] && msg="$msg($mc)" || msg="$msg(x)"
fi
done
if ! [ $testpattern ] && [[ "$nomatch" == "no" ]]; then
continue
fi
tmc=$(echo "$msg" | sed 's/x/0/g;s/)(/+/g')
[ $testpattern ] && let tmc=$tmc || tmc=0
if [[ "$addup" == "yes" ]]; then
(( $tmc $aucmp )) && msg="($tmc)" || msg="(x)"
[[ "$nomatch" == "no" ]] && [[ "$msg" == "(x)" ]] && continue
fi
let c=c+1
results[$c]="$msg $f"
if [[ "$quiet" == "no" ]]; then
echo -e "\r[$c]$msg $f "
fi
if [[ "$save" == "yes" ]]; then
echo $f >> $sfname
fi
done
if [[ "$save" == "yes" ]]; then
n=
echo -e "\rcreated file $sfname with $(cat $sfname | wc -l) records"
elif [[ "$quiet" == "no" ]]; then
echo -e "\r "
else
echo -n -e "\r \r"
fi
# filtering
[[ "$filteropt" =~ ^\s*$ ]] && exit 0
# sorting
records=$(printf "%s\n" "${results[@]}")
case "$filteropt" in
*RSC*|*RSORTBYCOUNT*)
records=$(echo "$records" | \
sed "s/(x)/(0)/g;s/[()]/ /g" | sort -n | \
sed "s/\b\([0-9][0-9]*\)\b/(\1)/g;s/)[ ]*(/)(/g" | \
sed 's/ (/(/;s/) /)/' | awk '{print "[" NR "]"$0}')
;;
*SC*|*SORTBYCOUNT*)
records=$(echo "$records" | \
sed "s/(x)/(0)/g;s/[()]/ /g" | sort -nr | \
sed "s/\b\([0-9][0-9]*\)\b/(\1)/g;s/)[ ]*(/)(/g" | \
sed 's/ (/(/;s/) /)/' | awk '{print "[" NR "]"$0}')
;;
*RSN*|*RSORTBYNAME*)
records=$(echo "$records" | \
sed "s/(x)/(0)/g" | \
awk -F'[ /]' '{print $NF ": " $0}' | \
sort | sed 's/^.*: //' | awk '{print "[" NR "]" $0}')
;;
*SN*|*SORTBYNAME*)
records=$(echo "$records" | \
sed "s/(x)/(0)/g" | \
awk -F'[ /]' '{print $NF ": " $0}' | \
sort -r | sed 's/^.*: //' | awk '{print "[" NR "]" $0}')
;;
*RSD*|*RSORTBYDATE*)
records=$(echo "$records" | sed "s/(x)/(0)/g" | \
awk -F'[ /]' '{A=$NF;gsub(/[A-Za-z.]/,"",A);print A ": " $0}'|\
sort | sed 's/^.*: //' | awk '{print "[" NR "]" $0}')
;;
*SD*|*SORTBYDATE*)
records=$(echo "$records" | sed "s/(x)/(0)/g" | \
awk -F'[ /]' '{A=$NF;gsub(/[A-Za-z.]/,"",A);print A ": " $0}'|\
sort -r | sed 's/^.*: //' | awk '{print "[" NR "]" $0}')
;;
esac
# selecting
case "$filteropt" in
*BN*|*BASENAME*)
filter="sed s|.*/||;s|\..*||"
;;
*FN*|*FULLNAME*)
filter="sed s|.*[[:space:]]||"
;;
*CSV*)
filter="sed s/)/,/g;s/(//g;s/[[:space:]]//g;s/\(.*\),\(.*\)/\2,\1/"
filter="${filter};s/\[[0-9]\+\]//"
;;
*)
filter="sed -n p;s/(x)/(0)/g"
esac
records=$(echo "$records" | $filter)
re='^[0-9]+$'
case "$filteropt" in
*HD*|*HEAD*)
n=$(echo "$filteropt" | sed 's/.*H[EA]*D\s*\([0-9][0-9]*\).*/\1/')
[[ ! "$n" =~ $re ]] && n=1
filter1="head -$n"
;;
*TL*|*TAIL*)
n=$(echo "$filteropt" | sed 's/.*T[AI]*L\s*\([0-9][0-9]*\).*/\1/')
[[ ! "$n" =~ $re ]] && n=1
filter1="tail -$n"
;;
*)
filter1="sed -n p"
;;
esac
records=$(echo "$records" | $filter1)
case "$filteropt" in
*GR*|*GREP*)
p=$(echo "$filteropt" | sed 's|.*GR[EP]*\s*/\(.*\)/.*|\1|')
filter2="grep $p"
;;
*)
filter2="sed -n p"
;;
esac
records=$(echo "$records" | $filter2)
echo "$records"
if [[ "$filteropt" =~ STAT ]]; then
tmp=$(printf "%s\n" "${results[@]}" | \
sed 's/(x)/(0)/g;s/)/,/g;s/(//g;s/[[:space:]]//g;s/\(.*\),\(.*\)/\1/')
[[ "$addup" == "yes" ]] && NPAT=1
for i in $(seq $NPAT); do
sq=$(echo "$tmp" | cut -d, -f$i)
vmin=$(echo "$sq" | awk 'NR==1 || $1<min {a=$1;min=$1}END{print a}')
vmax=$(echo "$sq" | awk 'NR==1 || $1>max {a=$1;max=$1}END{print a}')
avg=$(echo "$sq" | awk '{s+=$1}END{print s/NR}')
nz=$(echo "$sq" | awk '$1>0{s+=1}END{print s}')
echo "#$i: nonzeros=$nz, vmin=$vmin, vmax=$vmax, avg=$avg"
done
fi
case "$filteropt" in
*VF*|*VIEWFILES*)
tmp=$(echo "$records" | sed 's/^.* //')
c=0
for f in ${tmp[@]}; do
let c=c+1
echo [$c] $f
if [[ "${f##*.}" =~ pdf|PDF ]]; then
$PDFVIEW "$f"
else
$TXTVIEW "$f1.txt"
fi
done
;;
esac