#!/bin/ksh #******************************************************************************* # KSH PROGRAM #******************************************************************************* # # PROGRAM ID : block_grep.ksh # #D DESCRIPTION : Search for a string and display each occurrence #D including 2 lines before and 4 lines after this #D occurrence (2 and 4 are the defaults and can be #D changed with 3rd and 4th input parameter) #D # # INPUT PARAMETERS : $1 - $si_search_string (search string) # $2 - $si_file (name of the file) # $3 - $si_lines_before (number of lines to be displayed before) # $4 - $si_lines_after (number of lines to be displayed after) # # OUTPUT PARAMETERS : none # # # INPUT FILES : $si_file (name of the file) # # OUTPUT FILES : none # # # SPECIAL LOGIC NOTES : none # #******************************************************************************* # MODIFICATION LOG # # # DATE SE # DESCRIPTION # --------------------------------------------------------------------------- # Mon Dec 14 09:51:42 CET 2015 (Hajo) # initial version # --------------------------------------------------------------------------- # #******************************************************************************* #******************************************************************************* # INITIALIZATION #******************************************************************************* export s_parmcount=$# export s_myname=`basename $0` export s_parmcount_expected=2 if [[ $s_parmcount -lt $s_parmcount_expected ]] then print -u2 "${s_myname}: USAGE: ${s_myname} or" print -u2 " ${s_myname} !" exit 1 else export si_search_string=$1 export si_file=$2 export si_lines_before=$3 export si_lines_after=$4 if [ "${si_lines_before}" = "" ] then export si_lines_before=2 fi if [ "${si_lines_after}" = "" ] then export si_lines_after=4 fi typeset -i i_line_number=0 typeset -i i_line_number_before=0 typeset -i i_occurrence=0 fi #******************************************************************************* # FUNCTIONS #******************************************************************************* #-------------------------------------------------------------------------------- fun_block_grep () { #-------------------------------------------------------------------------------- typeset -i i_linecnt i_linecnt=0 typeset -i i_line_first=0 typeset -i i_line_last=$(wc -l ${si_file} | awk '{print $1}') typeset -i i_line_to_check=$1 typeset -i i_lines_before=${si_lines_before} typeset -i i_lines_after=${si_lines_after} typeset -i i_line_start=${i_line_to_check}-${i_lines_before} typeset -i i_line_end=${i_line_to_check}+${i_lines_after} if [[ ${i_line_start} -lt 0 ]] then i_line_start=0 fi if [[ ${i_line_end} -gt ${i_line_last} ]] then i_line_end=${i_line_last} fi cat ${si_file} | while read s_current_line do let i_linecnt=${i_linecnt}+1 if [[ ${i_linecnt} -ge ${i_line_start} ]] && [[ ${i_linecnt} -le ${i_line_end} ]] then echo "${s_current_line}" fi done } #******************************************************************************* # MAINLINE #******************************************************************************* grep -n ${si_search_string} ${si_file} | sed -e's/\:/ /' | awk '{print $1}' | while read i_line_number do #echo if [[ ${i_line_number_before} -ne ${i_line_number}-1 ]] if [[ ${i_line_number_before} -ne ${i_line_number}-1 ]] then let i_occurrence=${i_occurrence}+1 fun_block_grep ${i_line_number} echo "" echo "--------------------------------------------------------------------------------" echo "" fi i_line_number_before=${i_line_number} done export s_number_of_lines=$(grep ${si_search_string} ${si_file} | wc -l) echo "================================================================================" echo "---> Found ${i_occurrence} occurrences of <${si_search_string}>" echo "================================================================================" exit