LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-05-2009, 04:15 PM   #1
cyclegar
LQ Newbie
 
Registered: Mar 2005
Posts: 5

Rep: Reputation: 1
Script to grep log files based on system date


I would like help to write a script to grep a log file and count the no of lines based on a string and/or system date and mail the result.

For ex

Mon May 4 04:10:09 2009 ---x-x-x-x-
Mon May 4 15:44:27 2009 x-x-x--x-xx--x
Mon May 4 15:45:35 2009 xx=xxx-x-x--xx
Mon May 4 22:53:43 2009 xxx-x-x--x-xx
Tue May 5 07:27:18 2009 xxxx
Tue May 5 04:12:58 2009 abcd

In this log file i should be able grep no of lines for system date ( ex for May 5 its 2 and for may 4 its 4) also if possible grep no of lines for a string within the results of the first query by(date) and mail the result. for example for May 5 no of lines for string abcd will be 1.

Help in advance is appreciated.

Thanks
Mohammed
 
Old 05-05-2009, 04:42 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,001
Blog Entries: 54

Rep: Reputation: 2756Reputation: 2756Reputation: 2756Reputation: 2756Reputation: 2756Reputation: 2756Reputation: 2756Reputation: 2756Reputation: 2756Reputation: 2756Reputation: 2756
So your first stop would be 'man grep' for a basic understanding of what grep can do, then 'man date' for a basic understanding of how to get it to output your datestamp format. Then on to http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html and http://www.tldp.org/LDP/Bash-Beginne...tml/index.html. If you can post back the date string command and what switches grep uses to anchor a searchterm and count lines you're half way through. Good luck!
 
Old 05-05-2009, 06:08 PM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,284

Rep: Reputation: 172Reputation: 172
cron mails automatically as does at

also one can do something like this:

(pipe output to mail)
your_script | mailx -vs subject user

or
if you want to be clever you can embed commands in the mail like:
Code:
mailx -vs subject user <<EOF

The output of the script at: $(date)
is:
================
$(/path/to/script)
[or maybe directly grep]
$(grep this that)

EOF

I'lll leave the grep to you.

Last edited by bigearsbilly; 05-05-2009 at 06:11 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to delete mail based on date gquiring Linux - General 8 05-08-2013 09:24 AM
shell script to remove old files based on date WindozBytes Linux - General 12 06-04-2012 01:21 AM
Delete files based on date stefaandk Linux - General 3 06-17-2005 02:20 AM
Script for deleting files based on date MaverickApollo Linux - General 3 02-03-2004 07:54 PM
Listing Files based on date axero Linux - Newbie 2 10-19-2003 04:58 PM


All times are GMT -5. The time now is 02:11 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration