The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How do I get my script to monitor a new file using tail? lstorm2003 Shell Programming and Scripting 4 06-06-2009 09:58 AM
Aix script to monitor when a file has been updated elmesy AIX 2 03-19-2009 04:28 PM
Script to find file name for non matching pattern sujoy101 Shell Programming and Scripting 5 03-31-2008 10:10 AM
File search for pattern - script rahulrathod Shell Programming and Scripting 3 02-16-2007 04:03 AM
Find script with input pattern file iguanathompson Shell Programming and Scripting 8 02-06-2006 06:23 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-02-2009
rithick256 rithick256 is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 1
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 (permalink)  
Old 07-02-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,770
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 (permalink)  
Old 07-02-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,543
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
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:29 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0