Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 05-05-2009, 04:15 PM   #1
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.

Old 05-05-2009, 04:42 PM   #2
Registered: May 2001
Posts: 29,359
Blog Entries: 55

Rep: Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546
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 and 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
Senior Member
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

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

also one can do something like this:

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

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

The output of the script at: $(date)
[or maybe directly grep]
$(grep this that)


I'lll leave the grep to you.

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


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 > Forums > Non-*NIX Forums > Programming

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

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