Script to monitor the pattern in the log file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to monitor the pattern in the log file
# 1  
Old 07-02-2009
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
# 2  
Old 07-02-2009
try this
Code:
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
# 3  
Old 07-02-2009
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.
Code:
datepattern=`date +%a' '%b' '%d` 
sed .... | grep $datepattern

with GNU awk
Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I add a log file path to a vi file to monitor all the changes made to the file?

I'm curious to know how do I add an empty log file (test1.log) to an existing text file to monitor all the changes made to a.txt. Is this expression export PATH=$PATH:/home/test1.log right to be added to the text file a.txt? (5 Replies)
Discussion started by: TestKing
5 Replies

2. Shell Programming and Scripting

Script to monitor /var/log/messages

Hello All, I want to write a script to monitor my product logs from /var/log/messages and send notifications without using "tail -f" command.Please suggest alternatives and any other tools for monitoring and alerting. Thank You (1 Reply)
Discussion started by: Cva2568
1 Replies

3. Shell Programming and Scripting

Script to search for a pattern in 30 minutes from a log file

Hello All, I have to write a script which will search for diffrent patterns like "Struck" "Out of Memory" , etc from a log file in Linux box's. Now I will be executing a cron job to find out the results by executing the script once in every 30 minutes. suppose time is 14-04-29:05:31:09 So I... (3 Replies)
Discussion started by: Shubhasis Mathr
3 Replies

4. Shell Programming and Scripting

Monitor log file

Hi, I need to amend an existing ksh script so that it runs a process (stop weblogic) and in parallel needs to monitor a log file (startup.log) in the background for a certain string (e.g. unable to stop weblogic). If the string appears in the log i need to kill the stop weblogic process. ... (5 Replies)
Discussion started by: dholmaster
5 Replies

5. Shell Programming and Scripting

Script to monitor log file

Hi, Have written a script to monitor linux non standard log file based on line numbers, so each check store $otalinenum .. then in next check after 10 minutes it compre the current_total_line_num > last_total_line_num then it will parse the log file from last_total_line_num to... (0 Replies)
Discussion started by: Shirishlnx
0 Replies

6. HP-UX

Script to monitor /var/opt/resmon/log/event.log file

AM in need of some plugin/script that can monitor HP-UX file "/var/opt/resmon/log/event.log" . Have written a scrip in sh shell that is working fine for syslog.log and mail.log as having standard format, have interrogated that to Nagios and is working as I required . But same script failed to... (3 Replies)
Discussion started by: Shirishlnx
3 Replies

7. Red Hat

Need help with a shell script for finding a certain pattern from log file

Hell Guys, Being a newbie, I need some help in finding a certain string from a log file of thousands of lines (around 30K lines) and have the output in a separate file. Below is the file output - 10.155.65.5 - - "POST... (15 Replies)
Discussion started by: rockf1bull
15 Replies

8. Homework & Coursework Questions

shell script that can create, monitor the log files and report the issues for matching pattern

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write an automated shell program(s) that can create, monitor the log files and report the issues for matching... (0 Replies)
Discussion started by: itian2010
0 Replies

9. Shell Programming and Scripting

Need help for this monitor log script

this is the format of the log file in my system xxxxx_xxx_xxxx_xxxxx_09_10_2009_170457.log xxxx_xxx_2_0_09_10_2009_163834.log xxx_xxxxxxxxx_25_10_2009_045020.log xxx_xxxxxxx_08_11_2009_055728.log the path of this logs file in on for example /dellxmax/application/log what i want to do... (5 Replies)
Discussion started by: coxmanchester
5 Replies

10. UNIX for Dummies Questions & Answers

Log monitor script

Hi All, I have a question and hope someone will have an answer to that. I'm looking for a way to monitor log files being generated on a windows machine for some specified string and if its occurrence is found, raise an alarm though email. The log file size keeps on increasing and after a... (2 Replies)
Discussion started by: er_ashu
2 Replies
Login or Register to Ask a Question