Searching for Patterns in Files - kgrep

   

Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents
Appendix D.  Sample Korn Shell Scripts


Searching for Patterns in Files - kgrep

This is the Korn shell version of the Unix grep command. The b option is not supported, and the i flag causes multi-character expressions to be matched in both all upper-case or all lower-case (kgrep i AbC test matches AbC, abc, or ABC in test, but not aBc or other permutations). Here are the supported options:

c

display the number of lines that contain the given pattern

i

ignore case of letters during comparison (see above)

l

display only names of files with matching lines once

n

display the output with line numbers

s

do not display error messages

v

display all lines, except those that match the given expression

 #!/bin/ksh  #  #      kgrep - Korn Shell grep program  #  # Declare default flags  CFLAG= IFLAG= LFLAG= NFLAG= SFLAG= VFLAG=  integer LNUM=0 COUNT=0 TOT_COUNT=0  # Disable file name generation  set -f  # Check usage  if (($# < 2))  then        print "Usage: $0 [options] expression files"        exit 1  fi  # Parse command-line options  while true  do        case $1 in              -b* )  print "b option not supported" ;;              -c* )  CFLAG=1 ;;              -i* )  IFLAG=1 ;;              -l* )  LFLAG=1 ;;              -n* )  NFLAG=1 ;;              -s* )  SFLAG=1 ;;              -v* )  VFLAG=1 ;;              -* )          print "$0: unknown flag $1"                            exit 2 ;;              * )           PATTERN=$1                            shift                            break ;;        esac        shift  done  # Set no-print flags  NOPRINT=$VFLAG$CFLAG$LFLAG  V_NOPRINT=$CFLAG$LFLAG  # Set upper/lower pattern  typeset -u UCPATTERN=$PATTERN  typeset -l LCPATTERN=$PATTERN  # Check for file arg  if (($# == 0))  then        print "Must have file argument"        exit 1  fi  # Process files  for FILE  do       # Open file for standard input       exec 0<$FILE       # Read each line in file       while read -r LINE       do            # Increment line number counter            ((LNUM+=1))            # Check each line for the pattern            case $LINE in                  # See if PATTERN matches input                  *$PATTERN*)                        if [[ $VFLAG = "" ]]                        then                              [[ $NOPRINT = "" ]] &&  \  print -r "${NFLAG:+$LNUM:}$LINE"                              ((COUNT+=1))                        fi ;;                 # For -i option: See if                 # upper/lowercase patterns match                 *$UCPATTERN* | *$LCPATTERN*)                      if [[ $IFLAG != "" ]]                      then                           if [[ $VFLAG = "" ]]                           then                             [[ $NOPRINT = "" ]] &&  \  print -r "${NFLAG:+$LNUM:}$LINE"                             ((COUNT+=1))                           fi                      else                           if [[ $VFLAG != "" ]]                           then                             [[ $V_NOPRINT = "" ]] && \   print -r "${NFLAG:+$LNUM:}$LINE"                             ((COUNT+=1))                           fi                      fi ;;                 # For -v option: See if                 # pattern doesn't match                 !(*$PATTERN*))                      if [[ $VFLAG != "" ]]                      then                           [[ $V_NOPRINT = "" ]] &&  \  print -r "${NFLAG:+$LNUM:}$LINE"                           ((COUNT+=1))                      fi ;;            esac       done        # Process -l flag        # Display just the filename        # if there are matches in this file        if [[ $LFLAG != "" ]] && ((COUNT))        then              print - $FILE        fi        # Increment the total match counter        # for when kgrep'ing multiple files        ((TOT_COUNT+=COUNT))        # Reset the line number counter for the next file        LNUM=0        # Reset the match counter for the next file        COUNT=0  done  # Process -c flag  # Display the total number of matches found  [[ $CFLAG != "" ]] && print $TOT_COUNT  # Exit successfully  exit 0 

       
    Top
     



    Korn Shell. Unix and Linux Programming Manual, Third Edition
    Korn Shell. Unix and Linux Programming Manual, Third Edition
    ISBN: N/A
    EAN: N/A
    Year: 2000
    Pages: 177

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net