#!/bin/bash # Joerg Arndt's cgrep # ... online at http://www.jjj.de/ # your feedback is welcome mailto: arndt (AT) jjj.de # version: 2004-August-28 (13:42) #set -vx ################################## ## Examples: # cgrep -n PS1 ~/.profile # man tar | cgrep -n -C2 tape # locate howto | cgrep -i line # locate howto | cgrep -i '[^\/]*line.*\b' ## cpipe might be be better for this: # top -n 1 | cgrep -C999 bash # pstree | cgrep -C999 bash ################################## #GREP='grep' GREP='grep --line-buffered' IGNC=''; ## whether to ingnore case for x in "$@"; do case $x in -i) IGNC='i' continue ;; -*) ## ignore all other options continue; ;; *) ## first nonoption is pattern PAT=$x; break; ;; esac done if [ ! -t 1 ]; then ## if output is a pipe then do not colorize ${GREP} "$@"; else ## if interrupted set: # trap "echo -n ''" 0 ## color to black and attributes to normal trap "echo -n ''" 0 ## attributes to normal ## red: #${GREP} "$@" | sed "s/$PAT/&/g${IGNC}" ## bold: #e${GREP} "$@" | sed "s/$PAT/&/g${IGNC}" | sed "s/^\([^:]*:\)/&/g" ## background-cyan: ${GREP} "$@" | sed "s/$PAT/&/g${IGNC}" ## reverse: #${GREP} "$@" | sed "s/$PAT/&/g${IGNC}" # underline (works only under X11): #${GREP} "$@" | sed "s/$PAT/&/g${IGNC}" fi exit $? ############################################