Getting the date part from the log file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting the date part from the log file.
# 1  
Old 11-24-2011
Getting the date part from the log file.

Hi,
I have a script that runs to generate a log file which is in this format:

Start: Friday, April 29, 2011 18:30

User :
Host :
Database :

I need to write a script that reads this log file (RUN.LOG) and rename the existing log file by suffixing the name with the date(20110429RUN.LOG)Smilie.Any help on this will be highly appreciated.
# 2  
Old 11-24-2011
check the man page of date and awk/cut, using awk/cut u can get the date from the line Start and date with options -d +format will help u to get the date which can be used to prefix the file.

if u still get issue then ask us with the code which u tried.
# 3  
Old 11-24-2011
Hi Siva,
I have tried the following code
Code:
gdate=`echo $(head -2 RUN.LOG) | awk -F: '{print $2}'`;
echo "Date is :"$gdate

which gives the following output:
Code:
Date is : Friday, November 18, 2011 18

Now how do I get it in YYYYMMDD format after reading the first line of the log file.Kindly suggest . Thanks in advance.Smilie

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 11-24-2011 at 03:35 AM.. Reason: Please use code tags for code and data samples, thank you
# 4  
Old 11-24-2011
Code:
#! /bin/bash

date_line=`grep 'Start' RUN.LOG`
# date_line will contain "Start: Friday, April 29, 2011 18:30"
# Hope IFS is set to space on your system

dt=`echo $date_line | awk '{print $4}' | tr -d ','`
yr=`echo $date_line | awk '{print $5}'`
mnth=`echo $date_line | awk '{print $3}'`

case $mnth in
"January"   ) mnth='01' ;;
"February"  ) mnth='02' ;;
"March"     ) mnth='03' ;;
"April"     ) mnth='04' ;;
"May"       ) mnth='05' ;;
"June"      ) mnth='06' ;;
"July"      ) mnth='07' ;;
"August"    ) mnth='08' ;;
"September" ) mnth='09' ;;
"October"   ) mnth='10' ;;
"November"  ) mnth='11' ;;
"December"  ) mnth='12' ;;
esac

mv RUN.LOG $yr$mnth${dt}RUN.LOG

# 5  
Old 11-24-2011
try this...
Code:
gdate=`grep 'Start' RUN.LOG | awk -F: '{print $2}'`
echo "Date is : $gdate"

prefix=`date -d "$gdate" '+%Y%m%d'`
echo "Prefix is : $prefix"

mv RUN.LOG $prefix"RUN.LOG"

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 11-24-2011 at 04:54 AM.. Reason: Please use code tags for code and data samples, thank you
# 6  
Old 11-24-2011
Hi Siva,

I am getting error in the prefix part :
prefix=`date -d "$gdate" '+%Y%m%d'`
echo "Prefix is : $prefix"

error: date: Not a recognized flag: d

Need your help on this part.
Thanks
# 7  
Old 11-24-2011
it means -d option is not present in ur system.
check man page and try datecalc...

eg: datecalc "$gdate" pic yymmdd
or
datecalc "$gdate" pic yyyymmdd
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print the specific part of the file name with file creation date?

Hello Folks, I have an requirement, where i need to get total count of the file based on creation date with there filename selected pattern. Filename: MobileProtocol.20171228T154200.157115.udr I want to get the count of files created on each day based on a pattern find. find . -type... (7 Replies)
Discussion started by: sadique.manzar
7 Replies

2. UNIX for Dummies Questions & Answers

How to get year part from file created date?

Hi Gurus, I need to get year part of file created date. when using ls -l , it only show month, day and time. is there any option I can add to get year portition? Thanks in advance (7 Replies)
Discussion started by: ken6503
7 Replies

3. Shell Programming and Scripting

How do I take out(remove) the date part in the file name in a script?

Hi All, I need to create links between two directories. have multiple files in a specified location. Source Location ex: /opt/xdm/input/ Target Location ex: /opt/xdm input file names: 1. abc_app.aus.apac.yyyymmdd.dtd 2. abcd_app.aus.apac.yyyymmdd.dtd I need to build a code that reads... (1 Reply)
Discussion started by: kthri_82
1 Replies

4. Shell Programming and Scripting

How to remove the date part of the file?

Hi Gurus, I have file name like: abcd.20131005.dat I need to remove mid part of data final name should be abcd.dat thanks in advance (2 Replies)
Discussion started by: ken6503
2 Replies

5. Shell Programming and Scripting

Delete log files content older than 30 days and append the lastest date log file date

To delete log files content older than 30 days and append the lastest date log file date in the respective logs I want to write a shell script that deletes all log files content older than 30 days and append the lastest log file date in the respective logs This is my script cd... (2 Replies)
Discussion started by: sreekumarhari
2 Replies

6. Shell Programming and Scripting

Concatenate many files which contents the same date as part of name file

Gents, I have lot of files in a folder where each file name includes the date of generation, then I would like to merge all the files for each date in a complete file. list of files in forder. dsd01_121104.txt dsd01_121105.txt dsd01_121106.txt dsd03_121104.txt dsd03_121105.txt... (7 Replies)
Discussion started by: jiam912
7 Replies

7. Shell Programming and Scripting

how to update date part with new increment date time

hi experts, my requirement is like this i need to develop a shell script to update date part with new incremental date time in file some 'X' which is kept at some server location incrementing every two hours.as i am new to this scripting i need support from u people,thanx in advance (1 Reply)
Discussion started by: amanmro
1 Replies

8. Shell Programming and Scripting

Processing a log file based on date/time input and the date/time on the log file

Hi, I'm trying to accomplish the following and would like some suggestions or possible bash script examples that may work I have a directory that has a list of log files that's periodically dumped from a script that is crontab that are rotated 4 generations. There will be a time stamp that is... (4 Replies)
Discussion started by: primp
4 Replies

9. HP-UX

How do I take out(remove) the date part in the file name?

Hi Guys here I am again, I have two files in a specified location. Location ex: /opt/xdm/input/ input file names: 1. abc_app.yyyymmdd.dtd 2. abd_app.yyyymmdd.dtd I need to build a code that reads the files from the location based on the oldest date and cuts the date part... (5 Replies)
Discussion started by: ruthless
5 Replies

10. Shell Programming and Scripting

Add Date as part of file name

How can I add date in format yyyymmdd to my existing file name ? For example my file name is test.dat then i want it to be test.datyyyymmdd (3 Replies)
Discussion started by: amsh76
3 Replies
Login or Register to Ask a Question