The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Emergency UNIX and Linux Support !! Help Me!!
.
google unix.com



Emergency UNIX and Linux Support !! Help Me!! Post your urgent questions here for highest visibility. Posting a new thread to this forum requires Bits. We monitor this forum to help people with emergencies, but we do not guarantee response time or answers. This forum is "best effort" only. Members who reply to posts here receive a bonus of 1000 Bits per reply.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Finding a word at specific location in a string swapnil.nawale Shell Programming and Scripting 1 09-14-2009 08:49 AM
Word count of lines ending with certain word warlock129 Shell Programming and Scripting 8 08-30-2009 03:37 AM
finding the number of occurence of a word in a line priyanka3006 Shell Programming and Scripting 9 06-18-2009 07:55 AM
need help with finding a word in file's contents manmeet Shell Programming and Scripting 6 10-01-2008 02:21 PM
Finding a word in a file smr_rashmy Shell Programming and Scripting 10 02-13-2008 02:02 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 1 Week Ago
pssandeep pssandeep is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 34
Getting last 50 lines after finding a word

Hi All,

I have a requirement as below

I need find for a string in a log file and once i found that string i need to send a mail.Thsi can be done thru grep and mailx cmnd.

Here once i found the string i need to send last 50 lines(up) from where string found.

Actually they require to find out the user name which may be available before 50 lines.

One more thing this script will run every 15 min , i am thinking how to handle if there are multiple cases of these failures

any thoughts or code will be appreciated

Thanks
San
  #2 (permalink)  
Old 1 Week Ago
thegeek thegeek is offline
Registered User
  
 

Join Date: Apr 2009
Location: /usr/bin/vim
Posts: 431
1. grepping 50 lines after match.

Code:
grep -A 50 'PATTERN' file
2. For a script to be executed every 15 minutes.

You can use cron, and the cron job should be

Code:
*/15 * * * * CMD
  #3 (permalink)  
Old 1 Week Ago
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,710
I think he wants the lines before the pattern - grep -B 'pattern', assuming he has GNU grep (linux)
  #4 (permalink)  
Old 1 Week Ago
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,502
Code:
awk 'BEGIN{tail=50}
{
  a[NR%tail]=$0
}
/pattern/{ for(i=NR+1;i<=NR+tail;i++)print a[i%tail]}
' file
  #5 (permalink)  
Old 1 Week Ago
binlib binlib is offline
Registered User
  
 

Join Date: Aug 2009
Location: New Jersey
Posts: 55
A more efficient way would be to tail and follow the log with "tail -f", store the last 50 lines and email those lines when the failure word occurs. This can be done in either shell, awk or perl easily. An untested gawk version:

Code:
tail -f log-file |gawk '
BEGIN { email_cmd = "mail -s failure emails_addrs" }
{
  line[NR%50] = $0
}
/failure word/ {
  for (i = NR-49; i <= NR; i++)
    print line[i%50] | email_cmd
  close(email_cmd)
}'
  #6 (permalink)  
Old 1 Week Ago
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,884
I was going to suggest binlib's solution, above, although the standard "tail -f" command might not work after a log rotation. The tail from GNU's coreutils will follow the file opened, not the name of the file, unless you do:
Code:
  tail --follow=filename
But given that you want the most recent occurrence of a pattern before a second pattern occurs, try this (again, gawk or nawk or mawk):
Code:
tail --follow=log-file | awk '
    BEGIN { email_cmd = "mail -s failure emails_addrs" }
    BEGIN { last_found = "NOT AVAILABLE"; last_lineno = 0; }
    /username pattern/ {  last_found = $0; last_lineno = FNR; }
    /failure word/  { print "Found " (FNR-last_lineno) " lines ago:" last_found | email_cmd }'
You'll need to put your own stuff where you see italicized words
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 02:45 AM.


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