#!/bin/bash # colorized grep # Joerg Arndt's cgrep # ... online at http://www.jjj.de/ # your feedback is welcome mailto: arndt (AT) jjj.de # version: 2001-May-24 (12:39) #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 ################################## IGNC=''; ## whether to ingnore case XOPTS="$*" for x in $XOPTS; 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 $XOPTS; else ## if interrupted set: # trap "echo -n ''" 0 ## color to black and attributes to normal trap "echo -n ''" 0 ## attributes to normal ## red: #grep $XOPTS | sed "s/$PAT/&/g${IGNC}" ## background-cyan: grep $XOPTS | sed "s/$PAT/&/g${IGNC}" ## reverse: #grep $XOPTS | sed "s/$PAT/&/g${IGNC}" # underline (works only under X11): #grep $XOPTS | sed "s/$PAT/&/g${IGNC}" fi exit $? ############################################