help writing a script using date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help writing a script using date
# 1  
Old 01-06-2011
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

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

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


Last edited by radoulov; 01-06-2011 at 02:52 PM.. Reason: Code tags!
# 2  
Old 01-06-2011
Try:
Code:
grep "logs" /var/log/messages | grep $DATE > /var/tmp/logs

# 3  
Old 01-06-2011
Does not seems to be working Smilie

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

Last edited by vikbenq; 01-06-2011 at 03:33 PM..
# 4  
Old 01-06-2011
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).

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

# 5  
Old 01-06-2011
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...
# 6  
Old 01-06-2011
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:
Code:
DDMMM="`date '+%b %e'`"      # Month day-with-space
grep \^"${DDMMM}" /var/log/messages | grep "vikbenq" >/var/tmp/vikbenq_logs

Are we getting warmer?

Last edited by methyl; 01-06-2011 at 03:56 PM.. Reason: layout problem. Modify grep to start from beginning of line. Change output filename. A complete rewrite really!
This User Gave Thanks to methyl For This Post:
# 7  
Old 01-06-2011
Code:
DATE=$(date '+%b %m')
grep "logs" /var/log/messages | grep $DATE > /var/tmp/logs

OR use grep like (AS appropriate):
Code:
grep $DATE /var/log/messages > /var/tmp/logs

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I need help writing this script

:wall: Can't seem to figure out how to fix this please help its not starting over like I would like it to When I enter in "Date" or "Time" nothing comes Also if you can tell me the commands for the other 3 stuff that would be much appreciated #!/bin/bash clear while ; do echo... (8 Replies)
Discussion started by: nowruzr
8 Replies

2. Shell Programming and Scripting

Writing file name and date from LS command into a file to be imported into mysql

I am looking to do a ls on a folder and have the output of the ls be structured so that is is modificaiton date, file name with the date in a format that is compatible with mysql. I am trying to build a table that stores the last modification date of certain files so I can display it on some web... (4 Replies)
Discussion started by: personalt
4 Replies

3. Shell Programming and Scripting

Help in writing script

i need some help in donig some actions on files in a library. i want to get the n last files, and print to the screen their name, date, and how many times a specific string appears in each file.. how can i do this..?... (6 Replies)
Discussion started by: eee
6 Replies

4. Red Hat

writing a script

Dear Madam/Sir Who can help me with writing a script doing the following? 1- Read names of files (only files with special name format let say initially they have the same file name start like TT*) 2- Then create an empty files with the same names have been read in step one but with extension... (1 Reply)
Discussion started by: m.nageeb
1 Replies

5. Shell Programming and Scripting

writing condition using date stamp

Hi all, I am writing a script which i am executing using nohup command. while ( true ) do RequiredTime=06:00:00 SysTime=`echo $(date) | awk '{print $4}'` if ]; then body of script fi done this is executing 3 times at 6am. i want it execute the body of script... (3 Replies)
Discussion started by: firestar
3 Replies

6. Shell Programming and Scripting

Help in writing a script

Hey everyone Can anyone please write me a script to display numbers in descending order dynamically i.e if the user enter a number say 100 then the output should be like 100 99 ....so on till 0 I tried using the logic as for ((i =1; i<=100; i--) but the it goes into a infinite loop since even the... (7 Replies)
Discussion started by: icchi
7 Replies

7. Shell Programming and Scripting

Help me in writing the script

Hi, I have written a script which converts a give hexdecimal value to binary value in perl. But now, the problem is I should read every bit of it ( if its 10101010, i should read the value in each position and if the value in that position is 1 i should print a string and should exit if its... (1 Reply)
Discussion started by: prakashreddy
1 Replies

8. Shell Programming and Scripting

Writing the script

I whant write the script of monitoring my servers on night-active users. He must loging all user actions, when user login, when logout, when he do su etc. But i don't know with what to begin :( How command i can use? awk, sed, last And what logs i can use too? /var/log/messages, /var/log/secure... (1 Reply)
Discussion started by: jess_t03
1 Replies

9. Shell Programming and Scripting

Writing Script?

Anyone have an example of a simple shell script that solicits a (Y)es or (N)o response from the user. If the response is 'Y' display a message on the screen that thanks the user for the positive response. If the response is 'N' display a message that thanks the user for the negative response. If... (15 Replies)
Discussion started by: wmosley2
15 Replies

10. Shell Programming and Scripting

need help writing a script

Hello everyone. Well, I will get right to the point. I am new to Perl and trying to learn it as much as I can. I have been assigned the task of writing a perl script to extract information from firewall logs. Like I said, I am new to Perl and I am having a tough time because I think what I am... (3 Replies)
Discussion started by: tarballed
3 Replies
Login or Register to Ask a Question