Bash/Mgrep
From Attie's Wiki
(Difference between revisions)
(Created page with "This is a useful tool that extends grep. * Expression input is at the front, allowing quick & easy editing * Recursively locates all files named (e.g: <code>*.c</code>) * ...") |
m |
||
Line 2: | Line 2: | ||
* Expression input is at the front, allowing quick & easy editing | * Expression input is at the front, allowing quick & easy editing | ||
* Recursively locates all files named (e.g: <code>*.c</code>) | * Recursively locates all files named (e.g: <code>*.c</code>) | ||
− | * Multiple filenames may be specified | + | * Files checked defaults to '<code>*.c</code>' and '<code>*.h</code>', but can be easily changed |
+ | * Multiple filenames / masks may be specified | ||
* Uses <code>grep</code>'s extended regex mode | * Uses <code>grep</code>'s extended regex mode | ||
* Output shows file and line number, and is highlighted | * Output shows file and line number, and is highlighted | ||
* If the output flows over the screen, <code>less</code> allows you to review the results | * If the output flows over the screen, <code>less</code> allows you to review the results | ||
* If the output ''doesnt'' flow over the screen, <code>less</code> quits immediately | * If the output ''doesnt'' flow over the screen, <code>less</code> quits immediately | ||
+ | * Automatically detects if the output is to a terminal or pipe, utilizing <code>less</code> and highlighting for the former | ||
<source lang="bash"> | <source lang="bash"> | ||
#!/bin/bash | #!/bin/bash | ||
− | if [ $# -lt | + | if [ $# -lt 2 ]; then |
echo "usage:" | echo "usage:" | ||
− | echo " $0 <expression> <directory | + | echo " $0 <expression> <directory> [filename..]" |
exit 1 | exit 1 | ||
fi | fi | ||
Line 20: | Line 22: | ||
DIRECTORY="$1"; shift | DIRECTORY="$1"; shift | ||
− | FILES="-name '$1'"; shift | + | if [ $# -lt 1 ]; then |
− | while [ $# -ne 0 ]; do | + | FILES="-name '*.c' -or -name '*.h'" |
− | + | else | |
− | done | + | FILES="-name '$1'"; shift |
+ | while [ $# -ne 0 ]; do | ||
+ | FILES="${FILES} -or -name '$1'"; shift | ||
+ | done | ||
+ | fi | ||
+ | |||
+ | if [ -t 1 ]; then | ||
+ | PIPE=/tmp/$$.mgrep | ||
+ | trap "rm -f ${PIPE}" EXIT | ||
+ | mknod ${PIPE} p | ||
+ | less -FKnRX < ${PIPE} & | ||
+ | exec 1>&- | ||
+ | exec 1>${PIPE} | ||
+ | GREP_OPTIONS="--color=always ${GREP_OPTIONS}" | ||
+ | fi | ||
CMD="find \"${DIRECTORY}\" ${FILES}" | CMD="find \"${DIRECTORY}\" ${FILES}" | ||
eval ${CMD} | \ | eval ${CMD} | \ | ||
while read -r f; do | while read -r f; do | ||
− | grep - | + | grep -niH -E ${GREP_OPTIONS} "${EXPRESSION}" "${f}" |
− | done | + | done |
− | + | ||
</source> | </source> |
Revision as of 18:02, 12 October 2012
This is a useful tool that extends grep.
- Expression input is at the front, allowing quick & easy editing
- Recursively locates all files named (e.g:
*.c
) - Files checked defaults to '
*.c
' and '*.h
', but can be easily changed - Multiple filenames / masks may be specified
- Uses
grep
's extended regex mode - Output shows file and line number, and is highlighted
- If the output flows over the screen,
less
allows you to review the results - If the output doesnt flow over the screen,
less
quits immediately - Automatically detects if the output is to a terminal or pipe, utilizing
less
and highlighting for the former
#!/bin/bash if [ $# -lt 2 ]; then echo "usage:" echo " $0 <expression> <directory> [filename..]" exit 1 fi EXPRESSION="$1"; shift DIRECTORY="$1"; shift if [ $# -lt 1 ]; then FILES="-name '*.c' -or -name '*.h'" else FILES="-name '$1'"; shift while [ $# -ne 0 ]; do FILES="${FILES} -or -name '$1'"; shift done fi if [ -t 1 ]; then PIPE=/tmp/$$.mgrep trap "rm -f ${PIPE}" EXIT mknod ${PIPE} p less -FKnRX < ${PIPE} & exec 1>&- exec 1>${PIPE} GREP_OPTIONS="--color=always ${GREP_OPTIONS}" fi CMD="find \"${DIRECTORY}\" ${FILES}" eval ${CMD} | \ while read -r f; do grep -niH -E ${GREP_OPTIONS} "${EXPRESSION}" "${f}" done