/
idxgrep
executable file
·92 lines (83 loc) · 2.99 KB
/
idxgrep
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
#!/bin/bash
#===============================================================================
#
# FILE: idxgrep.sh
#
# USAGE: ./idxgrep.sh
#
# DESCRIPTION: search \index{.*} entries in TEX files
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Georg Wassen (georg.wassen@gmail.com),
# COMPANY:
# VERSION: 1.0
# CREATED: 31.07.2014 11:40:05 CEST
# REVISION: ---
#===============================================================================
function usage() {
echo
echo 'Grep in *.tex files inside \index{} macros'
echo "Usage:"
echo " $(basename $0) [parameters] [<regex>]"
echo " (may use egrep regular expressions)"
echo " If no <regex> is provided, using X11 clipboard content (xsel -b)"
echo " e.g.: $(basename $0) real.\?time (to get 'realtime', 'real-time' and 'real time')"
echo " Parameters: "
echo " -cN --context=N Show N lines of context around matches (default: 2)"
echo " -h --help Show this help"
echo " -x --exclude-comments Exclude searching in commented (deactivated) parts"
echo
}
ARGS=$(getopt -o 'c::hx' -l 'context::,help,exclude-comments' -- "$@") # parse parameters and store normalized string in $ARGS
eval set -- "$ARGS"; # set parameters to preprocessed string $ARGS
PARAM_SEARCHTERM=''
PARAM_CONTEXT=''
PARAM_PREFIX=''
while [[ $# -gt 0 ]]; do
#echo "1='$1'"
case "$1" in
-h|--help)
usage
exit
;;
-c|--context)
PARAM_CONTEXT='-2'
if [[ $2 ]]; then
PARAM_CONTEXT="-$2"
fi
shift
;;
-x|--exclude-comments)
PARAM_PREFIX='^[^%]*'
;;
--)
# ignore
;;
*)
# set as search term
PARAM_SEARCHTERM="$1"
;;
esac
shift
done
if [[ -z $PARAM_SEARCHTERM ]]; then
# no parameter: use clipboard buffer
PARAM_SEARCHTERM=$(xsel -b)
fi
# Regex: start with \index{
# then arbirary characters except '}'
# then the search term
# then again arbirary characters except '}'
# finally the closing '}'
PARAM_SEARCHTERM='\\index\{[^}]*'$PARAM_SEARCHTERM'[^}]*\}'
# first egrep: search in all TEX files, use color, show filename and line numbers
# second egrep: exclude files, where a '%' sign makes the previous match a comment
#egrep -n --color=always --ignore-case "$PARAM_SEARCHTERM" *.tex | egrep -iv "%.*$PARAM_SEARCHTERM"
# first grep: search for all occurences of $SEARCHTERM
# using $PREFIX (if set) to exclude commented lines
# color filename and line number, but unset coloring of match
# second grep: color matches to $SEARCHTERM (without $PREFIX)
GREP_COLORS='mt=' egrep --color=always -ni $PARAM_CONTEXT "$PARAM_PREFIX$PARAM_SEARCHTERM" *.tex | egrep --color=always -i -e '' -e $PARAM_SEARCHTERM