help writing a script using date

hello there ppl. happy new year to all

i am not an avid unix user and trying to resolve an issue which seems to be too tough for me..but would be way too easy for ya'll.

I am trying to pull the logs for last 24 hours using the date command. It works perfect with grep using this syntax: grep "logs" messages | grep "Jan 6", but I want this to run as a script so that I can set up a cron for this activity.

Below mentioned syntax is what I am trying but its not correct that is what I've established so far. please help. Thanx

#!/bin/bash
DATE=$(date +"%m-%d-%Y")

grep "logs"/var/log/messages | grep DATE > /var/tmp/logs

Try:

grep "logs" /var/log/messages | grep $DATE > /var/tmp/logs

Does not seems to be working :frowning:

If I use the below script it works but if I use $DATE instead of Jan 6 it would not work.

#!/bin/bash
DATE=$(date +"%m-%d-%Y")

grep logs /var/log/messages | grep "Jan 6" > /var/tmp/logs

I think this is what you mean. If the word "logs" appears in the lines you wish to extract, then I'm wrong.
Also I've tested with a different output filename in case there is a directory called /var/tmp/logs . The output filename is your choice.
The important part is the format of the output of the "date" command (yours produces mm-dd-yy format).

DDMMM="`date '+%b %e'`"      # Month day-with-space
grep "${DDMMM}" /var/adm/messages >/var/tmp/messages_extract

no this is not what i meant...

I am trying to pull the logs say vikbenq which would make more sense instead of using the word log.

#!/bin/bash
DATE=$(date +"%m-%d-%Y")

grep "vikbenq" /var/log/messages | grep "Jan 6" > /var/tmp/vikbenq_logs

Example of the log: It contains the date parameter in the beginning of the logs as well..
Jan 6 11:22:39 192.168.0.3 Jan 06 19:22:38 2011 vikbenq

I reiterate if I run the grep command it works perfect and I only get the logs for Jan 6 however I want to use this as a script where it should run everyday and pull the logs for only last 24 Hours.
i.e. If it ran today it would pull logs only for Jan 6 and if it runs tomorrow it would pull logs from Jan 7 and so on...

The log sample needs to be posted in Code Tags, but experience tells me that there are two spaces between "Jan" and "6".

Homing in on the requirement:

DDMMM="`date '+%b %e'`"      # Month day-with-space
grep \^"${DDMMM}" /var/log/messages | grep "vikbenq" >/var/tmp/vikbenq_logs

Are we getting warmer?

DATE=$(date '+%b %m')
grep "logs" /var/log/messages | grep $DATE > /var/tmp/logs

OR use grep like (AS appropriate):

grep $DATE /var/log/messages > /var/tmp/logs

@ methyl ----- Man we got HOT..u r a genius..

It worked just fine...thanx a lot to all for ur help. have a good one.

I used this and it worked:

#!/bin/bash
DDMMM="`date '+%b %e'`" # Month day-with-space
grep "vikbenq" /var/log/messages | grep \^"${DDMMM}" > /var/tmp/vikbenq_logs

Phew!

@anurag.singh
When using "grep" to search for a string containing space characters, remember to put double quotes round the string.