LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-11-2005, 08:53 AM   #1
Phaethar
Member
 
Registered: Oct 2003
Location: MN
Distribution: CentOS, Fedora
Posts: 182

Rep: Reputation: 30
Grep syntax questions


Hey all,

Just a general question about the use of the grep command. I can never seem to get it to work quite right, and the man pages for it don't really explain it very well. What I'm trying to do is have a daily job set up to pull the previous days worth of entries from various log files (httpd, system logs, etc). I try to test out various syntax combinations, but I can't ever seem to get grep to show just 1 days worth when sorting by date.

So, for example, the Apache logs store the date like this:
Code:
[10/Apr/2005:04:07:53 -0500]
I'm trying to view the previous days logs with something like this:
Code:
grep "date --date=yesterday '+%d/%b/%G/%T'" /var/log/httpd/access_log
With that, I get nothing returned, and I don't know what to do to sort it by date.

Also, if I can get my results to come back properly, I'd like to write them out to a text file with the date as the filename. I believe grep can pipe it to a file, but again, I'm not sure on the syntax. Can I just echo it to a file, or is there a better way?

If it helps any, this job will be running on a Fedora 3 system. Any help on this will really be appreciated.
 
Old 04-11-2005, 09:02 AM   #2
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
As a starting point change your date syntax, this is your current one:-
Code:
date --date=yesterday '+%d/%b/%G/%T'
10/Apr/2005/14:57:26
whereas what you want is:-
Code:
date --date=yesterday '+%d/%b/%G:%T'
10/Apr/2005:14:58:18
to match the way the Apache log stores the date.
 
Old 04-11-2005, 09:02 AM   #3
samel_tvom
Member
 
Registered: Aug 2004
Posts: 133

Rep: Reputation: 15
Hi!
I think that it could work if you tried to put $( ) around the thing that should return a string.
like this:
grep "$(date --date=yesterday '+%d/%b/%G/%T')" /var/log/httpd/access_log

does that work?
 
Old 04-11-2005, 09:24 AM   #4
Phaethar
Member
 
Registered: Oct 2003
Location: MN
Distribution: CentOS, Fedora
Posts: 182

Original Poster
Rep: Reputation: 30
Ok, it appears that I did in fact need the $( ) to return the values properly. I also took out the %T search variable to get it to work, thinking that it was only searching for the exact time, down to the second, as it is when the job runs. So, now my search string looks like this:

Code:
grep "$(date --date=yesterday '+%d/%b/%G')" /var/log/httpd/access_log
This returns all entries from yesterday, which is exactly what I wanted.

Now, how difficult will it be to write this to a text file at the same time?

Thanks!
 
Old 04-11-2005, 09:34 AM   #5
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
if you want it written to a file as well as the terminal, use the "tee" command. See "man tee" for the options available:-
Code:
grep "$(date --date=yesterday '+%d/%b/%G')" /var/log/httpd/access_log | tee -ai /path/file
If you dont want to see the results in the terminal just redirect the output:-
Code:
grep "$(date --date=yesterday '+%d/%b/%G')" /var/log/httpd/access_log > /path/file
will write it to a file, deleting the file first if it exists,
Code:
grep "$(date --date=yesterday '+%d/%b/%G')" /var/log/httpd/access_log >> /path/file
will append the results to the end of the file.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep ?? can grep us variables? DaFrEQ Linux - Software 4 09-14-2005 12:22 PM
Two bash syntax questions from Slackware boot zach_barrow Programming 6 03-11-2005 11:18 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM
Shells, editing and grep -- questions re: howto Jiawen Linux - Software 3 09-17-2003 03:00 PM
grep questions Chijtska Linux - General 4 02-14-2002 08:23 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:35 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration