Wicked Cool Shell Scripts
Wicked Cool Shell Scripts :: shell script 036-cgrep.sh

Shell Script 036-cgrep.sh

#!/bin/sh

# cgrep - grep with context display and highlighted pattern matches

context=0
esc=""   
bOn="${esc}[1m" bOff="${esc}[22m"
sedscript="/tmp/cgrep.sed.$$"
tempout="/tmp/cgrep.$$"

showMatches()
{
  matches=0

  echo "s/$pattern/${bOn}$pattern${bOff}/g" > $sedscript

  for lineno in $(grep -n "$pattern" $1 | cut -d: -f1)
  do 
    if [ $context -gt 0 ] ; then
      prev="$(( $lineno - $context ))"
      if [ "$(echo $prev | cut -c1)" = "-" ] ; then
        prev="0"
      fi
      next="$(( $lineno + $context ))"

      if [ $matches -gt 0 ] ; then
        echo "${prev}i\\" >> $sedscript
        echo "----" >> $sedscript
      fi
      echo "${prev},${next}p" >> $sedscript
    else
      echo "${lineno}p" >> $sedscript
    fi
    matches="$(( $matches + 1 ))"
  done

  if [ $matches -gt 0 ] ; then
    sed -n -f $sedscript $1 | uniq | more
  fi
}

trap "/bin/rm -f $tempout $sedscript" EXIT 

if [ -z "$1" ] ; then
  echo "Usage: $0 [-c X] pattern {filename}" >&2; exit 0
fi

if [ "$1" = "-c" ] ; then
  context="$2"
  shift; shift
elif [ "$(echo $1|cut -c1-2)" = "-c" ] ; then
  context="$(echo $1 | cut -c3-)"
  shift
fi

pattern="$1";  shift

if [ $# -gt 0 ] ; then
  for filename ; do 
    echo "----- $filename -----"
    showMatches $filename
  done
else
  cat - > $tempout	# save stream to a temp file
  showMatches $tempout
fi

exit 0

Explore The Book!
[book cover]
Table of Contents
Read Some Scripts!
Shell Script Library
Book Errata
All The Links
Read the Reviews
Talk About It
Author Bio
Buy The Book!



Other books by author Dave Taylor
Learning Unix for Mac OS X (O'Reilly & Associates)
Solaris 9 for Dummies (Wiley)
Teach Yourself Unix in 24 Hours (Sams/Macmillan)
Teach Yourself Unix System Administration in 24 Hours (Sams/Macmillan)
Creating Cool HTML 4 Web Pages (Wiley)
Ten Quick Steps to Learning Mac OS X Unix (ebook!)