Support LQ: Use code LQ3 and save $3 on Domain Registration
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, 05: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, 05:42 PM   #2
Registered: May 2001
Posts: 28,899
Blog Entries: 55

Rep: Reputation: 3357Reputation: 3357Reputation: 3357Reputation: 3357Reputation: 3357Reputation: 3357Reputation: 3357Reputation: 3357Reputation: 3357Reputation: 3357Reputation: 3357
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, 07:08 PM   #3
Senior Member
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,371

Rep: Reputation: 192Reputation: 192
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 07: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 10:24 AM
shell script to remove old files based on date WindozBytes Linux - General 12 06-04-2012 02:21 AM
Delete files based on date stefaandk Linux - General 3 06-17-2005 03:20 AM
Script for deleting files based on date MaverickApollo Linux - General 3 02-03-2004 08:54 PM
Listing Files based on date axero Linux - Newbie 2 10-19-2003 05:58 PM

All times are GMT -5. The time now is 08:46 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