Script to monitor the pattern in the log file

hi All,
how to find a pattern in the log file & display the above and below line
for example in the log file, i have many lines, whenever i search for "Category" it should display the above line with only few parameter like i want only the location name & department name
Thu Jul 02 11:05:23 2009<location> noth amer</location><Site>ohioc</site><department>IT sales</department><country>UAT</country>
Thu Jul 02 11:05:39 2009 ird: INFO: (pack) We have a Problem Record. pp_HPD: category, Type is invalid
Thu Jul 02 11:05:39 2009 please enter the corrct details

in the below code i'm able to get only the above line
Logn="file name"
for log in $Log
do
cat $log |sed -n -e'/Category/{x;1!p;}' -e h |grep "`date +%a' '%b' '%d`" >> $Logn/System_Log
done

but i'm not getting the custom parmater from the above line

try this

sed -n -e '/Category/{=;x;1!p;g;$!N;p;D;}' -e h $log |grep "`date +%a' '%b' '%d`" 

lose cat at the beginning. It is called UUOC - useless use of cat

use `date +%a' '%b' '%d` once is enough. don't put it together with grep. you don't want to call date everytime sed pass a line to grep.

datepattern=`date +%a' '%b' '%d` 
sed .... | grep $datepattern

with GNU awk

awk 'BEGIN{
 datepattern = "^"strftime("%a %b %d",systime())
}
/category/{
    if (x ~ datepattern){
        scrape(x)        
    }
    getline l
    if (l ~ datepattern){
        # do something with below line
    }   
}
{
 x=$0
}
function scrape(s){
        o=s
        gsub(/.*<location>/,"",o)       
        gsub(/<\/location>.*/,"",o)
        print "location: ",o
        gsub(/.*<department>/,"",x)
        gsub(/<\/department>.*/,"",x) 
        print "dept: "x
}' file