forked from chenkaie/Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwcgrep
executable file
·70 lines (64 loc) · 2.77 KB
/
wcgrep
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
#!/bin/bash
# Copyright 2004 Ben Reser <[email protected]>
# Licensed under the terms subversion ships under or GPLv2.
# Useful for greping in a subversion working copy.
# Essentially it behaves the same way your grep command does (in fact it
# ultimately calls the grep command on your path) with a few exceptions.
# Ignores the subversion admin directories (.svn) and vi(m) backup files.
# Recursive is always on with or without -r.
# Always print filename and line numbers.
# Ignores binary files.
# If no path is given the current working directory is searched not stdin.
# Other than that it will take any parameter or pattern your standard grep
# does.
#
# This script requires GNU findutils and by default GNU grep (though that
# can be changed with environment variables).
#
# There are three environment variables you can set that modify the default
# behavior:
#
# WCGREP_GREP Controls what command is used for the grep command.
# If unset or null wcgrep will use the command named grep.
# WCGREP_GREPARGS Controls what arguments are always passed to the grep
# command before the arguments given on the command line.
# If unset or null it defaults to -HnI (always print file
# names, line numbers and ignore binary files). If you wish
# to set no default args set the variable to a space (" ").
# WCGREP_IGNORE Controls what files are ignored by the grep command.
# This is a regex that is passed to the find command with
# -regex so see find's man page for details. If unset or
# null defaults to '.*~$\|.*/\.svn\(/\|$\)', which will
# ignore vim backup files and subversion admin dirs.
#define by Kent(2008/12/05), add
#-i: ignore-case
#--colour=auto: Surround the matching string with the marker find in GREP_COLOR environment variable (RECMOMMENDED)
WCGREP_GREPARGS="-HnIi --colour=auto --mmap"
WCGREP_GREP="grep"
WCGREP_IGNORE=".*/\.git\(/\|$\)\|.*/tags\|.*/cscope.out\|.*~$\|.*/\.svn.*\(/\|$\)"
if [ $# -eq 0 ]; then
echo "Usage: wcgrep PATTERN"
exit 0
fi
arg_count=$#
for (( i=1; i <= $arg_count; i++ )); do
arg="$1"
shift 1
if [ -z "$pattern" ]; then
if [ "$arg" == "--" ]; then
grepargs="$grepargs $arg"
pattern="$1"
shift 1
((i++))
elif [ "${arg:0:1}" != "-" ]; then
pattern="$arg"
else
grepargs="$grepargs $arg"
fi
else
pathargs="$pathargs $arg"
fi
done
find $pathargs -regex ${WCGREP_IGNORE:-'.*/\.git\(/\|$\)\|.*~$\|.*/\.svn\(/\|$\)'} -prune -o \
-type f -print0 | xargs -r0 ${WCGREP_GREP:-grep} ${WCGREP_GREPARGS:--HnI} \
$grepargs "$pattern" | nl -n ln | grep -Ii --colour=auto $grepargs "$pattern"