Convert filenames with DDMMYYYY to YYYYMMDD


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert filenames with DDMMYYYY to YYYYMMDD
# 1  
Old 04-24-2008
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 will convert all the dates in the filenames.

I have many files in the format of :

xxx_xxxxxxxx_DDMMYYYY_sometext.tar

and I want to change each of them to

xxx_xxxxxxxx_YYYYMMDD_sometext.tar

the "sometext" on the end isn't always the same amount of text. but the xxx_xxxxxxxx is always the same length.

I haven't gotten anywhere on this script, been reading tutorials and through the forums and I can't figure out even where to start.

I'd assume it'd be some kind of loop reading in each file until there are no more, and then using a variable to capture DD MM and YYYY, and then doing a mv on the file to YYYYMMDD. Logically I have it, but it's actually writing it that is my problem.
# 2  
Old 04-24-2008
tmp=xxx_xxxxxxxx_DDMMYYYY_sometext.tar

echo $tmp | awk -F"_" '{ print $1"_"$2"_"substr($3,5,4)substr($3,3,2)substr($3,1,2)"_"$4}'

output of this is

xxx_xxxxxxxx_YYYYMMDD_sometext.tar
$

i think this would help u

Last edited by aju_kup; 04-24-2008 at 11:36 AM..
# 3  
Old 04-24-2008
Code:
#!/bin/ksh
find . -type f -name '*??_????????_[0-9][0-9]*_*.tar' | nawk -v q="'" -F_ -v OFS='_' '
{
  f=$0
  $3=substr($3,5) substr($3,3,2) substr($3,1,2)
  printf("mv %c%s%c %c%s%c\n", q, f, q, q, $0, q)
}'

# 4  
Old 04-24-2008
Quote:
Originally Posted by vgersh99
Code:
#!/bin/ksh
find . -type f -name '*??_????????_[0-9][0-9]*_*.tar' | nawk -v q="'" -F_ -v OFS='_' '
{
  f=$0
  $3=substr($3,5) substr($3,3,2) substr($3,1,2)
  printf("mv %c%s%c %c%s%c\n", q, f, q, q, $0, q)
}'

./testScript.sh
./testScript.sh[7]: nawk: not found


Any way of semi-explaining how that script works and what all that stuff means. I can't quite find a tutorial that goes that in depth.
# 5  
Old 04-24-2008
use 'awk' instead of 'nawk'. (if you have 'gawk' - use that).
Try it first.
# 6  
Old 04-24-2008
Quote:
Originally Posted by vgersh99
use 'awk' instead of 'nawk'. (if you have 'gawk' - use that).
Try it first.
Did that, and it ran, but when it runs it prints out the line, but it doesn't actually rename the files. And also, instead of the format being:

YYYYMMDD it's YYYYDDMM

just in case my acronyms are backwards y=year m=month(2 digit month, so january = 01), dd=2 digit day(1st=01)
# 7  
Old 04-24-2008
sorry - my bad:
Code:
#!/bin/ksh
find . -type f -name '*??_????????_[0-9][0-9]*_*.tar' | nawk -v q="'" -F_ -v OFS='_' '
{
  f=$0
  $3=substr($3,5) substr($3,1,2) substr($3,3,2)
  printf("mv %c%s%c %c%s%c\n", q, f, q, q, $0, q)
}'

This script does not rename the files - it just outputs the 'renaming' commands.
Let's make sure the 'mv' commands are correct.

After we're satisfied with that:
Code:
./testScript.sh | sh

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Convert string (YYYYMMDD) format to date in Sun OS

Hi All I need help in converting a string of YYYYMMDD format to date in Sun OS and then find out if the day is a Wednesday or not. The "date -d" option is not working and your help is much appreciated. The date command usage from the operating system we use here is as follows: Thanks, SK (11 Replies)
Discussion started by: SK123
11 Replies

2. Shell Programming and Scripting

Convert string (YYYYMMDD) format to date in Sun OS

Hi All I need help in converting a string of YYYYMMDD format to date in Sun OS and then find out if the day is a Wednesday or not. The "date -d" option is not working and your help is much appreciated. The date command usage from the operating system we use here is as follows: usage: ... (1 Reply)
Discussion started by: SK123
1 Replies

3. Shell Programming and Scripting

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. Novawise_Activity_Call_Notes_04022013.txt Novawise_Activity_Inbound_04022013.txt Novawise_Activity_Inbound_05022013.txt Novawise_Activity_Call_Notes_05022013.txt... (8 Replies)
Discussion started by: djrulz123
8 Replies

4. Shell Programming and Scripting

date(ddmmyyyy) sorting

input : 20110730 20110730 20110731 20110731 20110801 20110801 20110801 20110813 20110815 01062011 01062011 OUTPUT : i need to sort this input in such a way so that the latest date comes first. (11 Replies)
Discussion started by: urfrnddpk
11 Replies

5. AIX

Convert time (YYYYMMDD HHMMSS) to UTC

Okay, so let's say we have a string like: 20110105_193345 This represents: January 5th, 2011 = 20110105 24-hour style time 19:33:45 = 193345 Okay, so we have our time. It's January 5th, 2011 at 19:33:45. I want to convert this time from Eastern Time Zone (which it currently is in)... (1 Reply)
Discussion started by: syndex
1 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. Shell Programming and Scripting

Using awk to convert DD-MMM-YY to YYYYMMDD

Hi i need to convert a date in the format DD-Mon-YY to YYYYDDMM Ex : 01-JUL-00 to 20000701 Can anybdy help me with this?? Thanks in advance Shenaz (5 Replies)
Discussion started by: shanu_85
5 Replies

8. Shell Programming and Scripting

How to convert DDMMYYYY to DD MONTH YYYY in Unix

Hi I am having date as a string in DDMMYYYY format(07082008) in a variable say cdate. I want to Convert it into DD Month YYYY format(7 August 2008). Could someone help. Thanks in Advance. (2 Replies)
Discussion started by: rspk_praveen
2 Replies

9. UNIX for Dummies Questions & Answers

how to convert the string YYYYMMDD into YYYY.MM.DD

how to convert the string YYYYMMDD into YYYY.MM.DD Please advice (1 Reply)
Discussion started by: spatra
1 Replies

10. Shell Programming and Scripting

ddmmyyyy to dd-mmm-yy format?

Hi All, Can anyone tell me a simple way of converting a date in ddmmyyyy format to dd-mmm-yy format. For example 17022006 to 17-FEB-06 Thanks in advance Regards, Gaurav (11 Replies)
Discussion started by: gauravgoel
11 Replies
Login or Register to Ask a Question