So I dusted it off and cleaned it up a bit. Works as bash as well as ksh.
TERM setting is important as it highlights the line on which the phrase
occurs. You would probably want to use it as "kwic [Ee]rror 5 logfile"
While editing the file (with vi) is definitely easier to look around with,
it does load the entire file into memory - THAT can be expensive.
j.
--- kwic ---
#!/bin/ksh
########################################################
#
# Key Word In Context
#
# Given a string or regular expression, find all occurences of the string
# in the current directory and display the filenames
# and the surrounding 'n' lines. 'n' defaults to 10.
#
# Usage: kwic phrase [n] [file]
#
# n defaults to 10, file defaults to all files in the current directory
#
# Example kwic [Ee]rror 10 filelist -- Upper or lower case error
# kwic "[Hh]i [Mm]om" -- with a space
# kwic ^$ 5 filelist -- look for blank lines
#
# Jack Parker 1994
#
########################################################
phrase=$1 # regular expression that we are looking for
if [ $# -lt 2 ]
then
n_lines=10 # Number of lines to display on either side
else
n_lines=$2
fi
if [ $# -gt 2 ] # user has requested specific file(s)
then
shift
shift
filelist=$*
else # find all filenames where the phrase occurs
filelist=`grep "$phrase" * | awk -F":" '{print $1}' | sort -u `
fi
for f in $filelist # within that list of files
do
occ=`grep -n "$phrase" $f | awk -F":" '{print $1}'` # Find line numbers
for n in $occ # For each occurence
do
if [ $n -gt $n_lines ] # set start and end
then
let beg=$n-$n_lines
else
let beg=0
fi
# seperator
print "============================================================"
print "FILENAME: $f"
tail +$beg $f | head -$n_lines # Start n lines before
tput smso # turn on highlight
tail +$n $f | head -1 # print the line in question
tput rmso # turn off highlight
let s=$n+1
tail +$s $f | head -$n_lines # show the next 'n' lines
# and you want to do what now?
printf "\nPress return to continue, (q)uit, (n)ext file, (e)dit:\n"
read junk </dev/tty
case $junk
in
"q"|"Q" ) # allow them to enter in either case (quit)
exit 0;;
"n"|"N" ) # next file
break;;
"e"|"E"|"v"|"V" ) # yes, we said 'e', but how many of us still hit 'v'?
# the + takes vi directly to the line in question
vi +$n $f
;;
esac # must be return, keep on truckin
done
done
--- cut here ---
-----Original Message-----
From: Sikyala Raquel
[mailto:oracledba-ezmlmshield-x43888236.[Email address protected]
Sent: Thursday, February 01, 2007 11:58 AM
To: LazyDBA Discussion
Subject: using find and find next in unix
I am trying to search a log file for errors. The file is huge. What is
the command to search for every occurance of the word error and be able
to continue to the end of the file?
Raquel Sikyala
---------------------------------------------------------------------
TO REPLY TO EVERYBODY , PLEASE CLICK REPLY-ALL, NOT JUST REPLY
To post a dba job: http://jobs.lazydba.com
To Subscribe : http://www.LazyDBA.com
To unsubscribe: http://www.lazydba.com/unsubscribe.html
Oracle LazyDBA home page