![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
||||
|
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 |
|
||||
|
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)
}'
|
|
|||||
|
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 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 }'
|
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|