Here below a GREP that provide you x lines before and y lines after. Simple and easy !
#!/bin/ksh
USAGE= »Usage: $0 [-b numlinesbef] [-a numlinesaft] word file »
WRONG=0
NOPT=0
LBEF=0
LAFT=0while getopts :b:a: OPT
do
case $OPT in
b)
LBEF=$OPTARG
((NOPT = NOPT + 2))
;;
a)
LAFT=$OPTARG
((NOPT = NOPT + 2))
;;
\:\)
WRONG=1
echo « $OPTARG option requires an argument »
;;
\?)
WRONG=1
echo « $OPTARG is not a valid option. »
echo « $USAGE »
;;
esac
doneshift $NOPT
if [ $# -ne 2 ] || ((WRONG == 1)) ; then
echo « $USAGE »
exit 2
fiTOKEN=$1
FILENAME=$2if [ ! -e $2 ] ; then
echo « File [$2] does not exist ! »
exit 2
fi
NOCCUR=0
for SLINE ingrep -n $TOKEN $FILENAME | nawk -F : '{ print $1 }'
do
((TOPL = SLINE – LBEF))
((BOTL = SLINE + LAFT))if (( $TOPL < 1 )) ; then TOPL=1 fi LINEC=
wc -l $FILENAME | nawk ‘{ print $1 }’
if (( $BOTL > $LINEC )) ; then
BOTL=$LINEC
fi
sed -n « ${TOPL},${BOTL}p » $FILENAME
((NOCCUR = NOCCUR + 1 ))
echo « —————————————————————– »
done
echo
echo « The word [${TOKEN}] was found ${NOCCUR} times in the file [${FILENAME}] »
echo
exit 0
>
Comments are closed, but trackbacks and pingbacks are open.