#!/bin/ksh ## ## SCRIPT: grep_10lines_around ## ## in $FEDIR/scripts where $FEDIR=/apps/nns_com/fea ## ## (May rename to grep3 someday.) ############################################################################# ## PURPOSE: Shows ten lines both above and below lines with a match to ## the string in $1. The file being searched is named in $2. ## ############################################################################# ## CALL FORMAT: $FEDIR/scripts/grep_10lines_around ## ## Can test with ## ./grep_10lines_around tk $HOME/.profile ## or ## ./grep_10lines_around tool $HOME/.auxchestrc ############################################################################# ## CALLED BY: manually (and someday in a utility, like a FileTools GUI menu ## or in nnsFEAmenu 'C') ## ## Can use an alias in $HOME/.profile, like ## alias grep10='/apps/nns_com/fea/scripts/grep_10lines_around' ############################################################################# ## MAINTENANCE HISTORY: ## Written by : Blaise Montandon O06 18Oct99 Based on 'grep_3lines_around' ## Last Update: Blaise Montandon O06 18Oct99 ############################################################################# ############################################################################# ## Check for two input items. ############################################################################# if test "$1" = "" then echo " INPUT ERROR: Supply a string (and filename) to script $0. Input: Exiting ... " exit fi if test "$2" = "" then echo " INPUT ERROR: Supply a filename to script $0. Input: Exiting ... " exit fi ## For testing: # set -x ############################################################################# ## Here is a possibly handy SIMPLE EXAMPLE awk program to ## write all lines whose first field is different from the previous one. ## ## $1 != prev { print; prev = $1 } ## ## This grep-extended script is basically a more complex version of this. ############################################################################# echo " *................................................. * Lines in file $2 * that match the string '$1' --- * including 10 lines above-and-below matches. * (All lines are preceded by line numbers and an * asterisk before a line-number indicates a match.) *................................................. " awk -v STRING="$1" \ 'BEGIN { ################################################## ## The 10 'prev' vars hold the 10 last lines read. ################################################## prev1 = "" prev2 = "" prev3 = "" prev4 = "" prev5 = "" prev6 = "" prev7 = "" prev8 = "" prev9 = "" prev10 = "" ################################################## ## 'aftcount' holds the integers 10,9,...,2,1, or 0 ## --- representing the number of lines after the ## last matched line that need to be printed. ################################################## aftcount = 0 ################################################## ## 'lastprt' holds the line# of the line last printed. ## 'lastprt' is reset any time 'printf' is called. ################################################## lastprt = 0 } # end of BEGIN #################################################### ## IF WE HAVE A MATCH, SUSPEND PRINTING ## THREE 'AFTER-A-MATCH-LINES': ## If there is a new match, reset 'aftcount' to zero. ## (We do not want to print a line twice.) ## We will restart aftcount at 3 after the new match ## line is printed. #################################################### $0 ~ STRING \ { aftcount = 0 } ###################################################### ## PRINT ONE OF THE 3 'AFTER-A-MATCH-LINES': ## If 'aftcount' is non-zero, print the current line. ## We had a match up to 3 lines ago. Decrement 'aftcount' ## and save the number of the printed line in 'lastprt'. ###################################################### ( aftcount != 0 ) \ { aftcount = aftcount - 1 ; printf (" %s %s \n", NR, $0); lastprt = NR } ###################################################### ## IF WE HAVE A MATCH, PRINT 3-PREV & CURRENT: ## If there is a match, print the 3 previous lines ## --- as long as their linenums are greater than ## the last-printed line number. (We do not want ## to print a line twice.) ## ## Then print the current line. Also set 'aftcount' ## to three, and save the ## number of the matched-printed line in 'lastprt'. ###################################################### { recnum = NR - 10 } ( $0 ~ STRING && recnum > lastprt ) \ { printf (" %s %s \n", recnum, prev10); } { recnum = NR - 9 } ( $0 ~ STRING && recnum > lastprt ) \ { printf (" %s %s \n", recnum, prev9); } { recnum = NR - 8 } ( $0 ~ STRING && recnum > lastprt ) \ { printf (" %s %s \n", recnum, prev8); } { recnum = NR - 7 } ( $0 ~ STRING && recnum > lastprt ) \ { printf (" %s %s \n", recnum, prev7); } { recnum = NR - 6 } ( $0 ~ STRING && recnum > lastprt ) \ { printf (" %s %s \n", recnum, prev6); } { recnum = NR - 5 } ( $0 ~ STRING && recnum > lastprt ) \ { printf (" %s %s \n", recnum, prev5); } { recnum = NR - 4 } ( $0 ~ STRING && recnum > lastprt ) \ { printf (" %s %s \n", recnum, prev4); } { recnum = NR - 3 } ( $0 ~ STRING && recnum > lastprt ) \ { printf (" %s %s \n", recnum, prev3); } { recnum = NR - 2 } ( $0 ~ STRING && recnum > lastprt ) \ { printf (" %s %s \n", recnum, prev2); } { recnum = NR - 1 } ( $0 ~ STRING && recnum > lastprt ) \ { printf (" %s %s \n", recnum, prev1); } $0 ~ STRING \ { printf ("*%s %s \n", NR, $0); aftcount = 10; lastprt = NR } ###################################################### ## Update prev10, prev9, ..., prev3, prev2, and prev1, ## before reading the next line. ###################################################### { prev10 = prev9; prev9 = prev8; prev8 = prev7; } { prev7 = prev6; prev6 = prev5; prev5 = prev4; prev4 = prev3; } { prev3 = prev2; prev2 = prev1; prev1 = $0; } ' $2 # }' $2 > $HOME/temp.lis