Date format to be changed from DDMMYYYY to YYYYMMDD


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date format to be changed from DDMMYYYY to YYYYMMDD
# 1  
Old 03-12-2013
Date format to be changed from DDMMYYYY to YYYYMMDD

My requirement is:- there will be files at a location each day with the date format DDMMYYYY.
Code:
Novawise_Activity_Call_Notes_04022013.txt
Novawise_Activity_Inbound_04022013.txt
Novawise_Activity_Inbound_05022013.txt
Novawise_Activity_Call_Notes_05022013.txt
Novawise_Activity_Call_Notes_06022013.txt
Novawise_Activity_Inbound_06022013.txt

Now in another file we store the last run date (LAST_DATE.TXT) in the format 20130405(YYYYMMDD). The current value is 20130405.

I want to pick up only the files for the 6th and not the 4th and the 5th. And then store the date 06022013 (from the file) into the LAST_DATE.TXT file in the format 20130406.
Hence i would process each day only those files which are latest and after the last run date.

Please let me know how this can be done.

Last edited by radoulov; 03-12-2013 at 07:34 AM..
# 2  
Old 03-12-2013
As your specification is utterly vague, it's difficult to suggest a solution. I'd propose you use sed or awk to transpose the single elements of your date field.
# 3  
Old 03-12-2013
in order to find out the date of the files i have used the below code

ls|rev|cut -d "." -f2|cut -d "_" -f1|rev

this gives me the dates of all the files in the format ddmmyyyy.

now if i can change this to YYYYMMDD then my problem is solved. I would be able to bring the files which have this above calculated date greater than the last run date.
can this be done??

---------- Post updated at 01:27 PM ---------- Previous update was at 01:17 PM ----------

if the date can be changed to YYYYMMDD then i can use the code below to bring the relative files.

Code:
set -vx
TARGET_DIR=$2
SOURCE_DIR=$1
LOGDIR=/info_d05/visage/SrcFiles/IQAlign/Log_Files
b=`cat $LOGDIR/last_date.txt`
for i in $SOURCE_DIR/Novawise*
do
 fname=`basename $i`
 p=`echo $fname|rev|cut -d "." -f2|cut -d "_" -f1|rev|something`
  if [ $p -gt $b ]
  then
   cp $SOURCE_DIR/$fname $TARGET_DIR/
  fi
  
done

the something part needs to be completed...

Last edited by radoulov; 03-12-2013 at 07:17 AM.. Reason: Code tags fixed.
# 4  
Old 03-12-2013
This is just a crude suggestion; use it as a starting point:
Code:
$ ls | awk -F[_.] '{print substr($(NF-1),5,4) substr($(NF-1),3,2) substr($(NF-1),1,2)}'
20130204
20130204
20130205
20130205

or:

Code:
ls | awk -F_ '{$NF= substr($NF,5,4) substr($NF,3,2) substr($NF,1,2) substr($NF,9)}1' OFS="_"

EDIT: added the ls | to the second proposal.

Last edited by RudiC; 03-12-2013 at 05:51 AM.. Reason: making second proposal clearer.
# 5  
Old 03-12-2013
well the first one worked perfectly. but the second one did not..i am running the entire script and would let u know the outcome.i am using the first one. But i didn't understand the functioning. can you please explain how this worked?
# 6  
Old 03-12-2013
You have to prepend whatever input you produce to the second one, of course, like the ls | used above. I added it to the code.

The first code breaks the input line at _ and . as told by the field separator, set with -F. $NF is the last field ("txt"), $(NF-1) the before last field containing the date string. The use of the substr function is self explanatory, I think.
The second works similarly, but leaves the .txt extension intact.

Last edited by RudiC; 03-12-2013 at 05:50 AM.. Reason: typo corrected
# 7  
Old 03-12-2013
well thanks a lot for the help...the code works perfectly...this was a part of the huges script in which i was struck...now the entire thing is working as expected..i had little knowledge of awk programming so didn't understand it in the beginning...can you help me out with a good pdf or something to read from and understand awk and nawk...would appreciate your help....thanks..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command to check date format DDMMYYYY

Record: Record1|Record2|Record3|Record4|Record5|DATE1|DATE2 Need to Check DATE1 & DATE2 is in DDMMYYYY format in a file. records which not meet the date format DDMMYYYY extract to other file. (1 Reply)
Discussion started by: vivekn
1 Replies

2. Shell Programming and Scripting

Validating date in yyyymmdd format using PERL

Hi all, i had a code where in user will enter a date in yyyymmdd format.. i didnt use any validation for the date and now the problem is if a user enters date instead of month after year it is proceeding with the code.. like if the date is 20120426 and if the user enters 20122604 it... (4 Replies)
Discussion started by: smarty86
4 Replies

3. Shell Programming and Scripting

Date after 5 days from current date in YYYYMMDD format

Hello Experts, How do i get date after 5 days from current date in YYYYMMDD format? How do you compare date in YYYYMMDD format? Thanks (8 Replies)
Discussion started by: needyourhelp10
8 Replies

4. Solaris

Date after 5 dates in YYYYMMDD format

Hi Experts, How to get date 5 days after current date in YYYYMMDD format? How do we compare date in YYYYMMDD format? Thanks (1 Reply)
Discussion started by: needyourhelp10
1 Replies

5. Shell Programming and Scripting

Change the date format from mmddyyyy to yyyymmdd

How do I change the date format from mmddyyyy to yyyymmdd in PERL. Can any one help me please. (3 Replies)
Discussion started by: thankful123
3 Replies

6. Shell Programming and Scripting

convert date format YYYYMMDD to MM/DD/YYYY

In my shell script i have a variable which stores date in the format of YYYYMMDD. Is there any way to format this value to MM/DD/YYYY. Thanks. (8 Replies)
Discussion started by: nasirgondal
8 Replies

7. UNIX for Dummies Questions & Answers

Format date from MM/DD/YYYY to YYYYMMDD

I have a file with some date columns in MM/DD/YYYY format: SMPBR|DUP-DO NOT USE|NEW YORK||16105|BA5270715|6/6/2007 |MWERNER|109||||JOHN||SMITH|MD|72211118||||||74559|21 WILMINGTON RD||D|11/6/2003|SL# MD CONTACT-LIZ RICHARDS|||0|Y|N||1411458| And I want to convert the date format to: ... (5 Replies)
Discussion started by: ChicagoBlues
5 Replies

8. Shell Programming and Scripting

Convert filenames with DDMMYYYY to YYYYMMDD

Okay, I have posted on here one other time and received very fast help, so I figured I'd post again. Searched for awhile and couldn't find exactly what I'm looking for. I am attempting to write a script that will search through a given directory, or search through the current directory, and... (10 Replies)
Discussion started by: cbo0485
10 Replies

9. HP-UX

How can i get the yesterday's date in YYYYMMDD format

How can i get the yesterday's date in YYYYMMDD format??? (4 Replies)
Discussion started by: prasadsr
4 Replies

10. UNIX for Dummies Questions & Answers

get yesterday date in yyyymmdd format

I would like to know how I could get a yesterday date in yyyymmdd e.g. today is 20011109, and I would like to get 20011108. Thank you!:confused: (2 Replies)
Discussion started by: hk_newbie
2 Replies
Login or Register to Ask a Question