The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
convert date format YYYYMMDD to MM/DD/YYYY nasirgondal Shell Programming and Scripting 7 08-07-2008 06:06 AM
get yesterday in yyyymmdd format aaron_fong Shell Programming and Scripting 13 03-14-2008 12:14 PM
ddmmyyyy to dd-mmm-yy format? gauravgoel Shell Programming and Scripting 11 01-12-2008 09:16 PM
Converting YYYYMMDD to Julian dfran1972 Shell Programming and Scripting 5 04-28-2005 07:34 AM
get yesterday date in yyyymmdd format hk_newbie UNIX for Dummies Questions & Answers 2 12-14-2001 12:32 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 04-24-2008
Registered User
 

Join Date: Mar 2008
Posts: 30
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.
Reply With Quote
Forum Sponsor
  #2  
Old 04-24-2008
Registered User
 

Join Date: Jul 2006
Posts: 189
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 07:36 AM.
Reply With Quote
  #3  
Old 04-24-2008
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,019
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)
}'
Reply With Quote
  #4  
Old 04-24-2008
Registered User
 

Join Date: Mar 2008
Posts: 30
Quote:
Originally Posted by vgersh99 View Post
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.
Reply With Quote
  #5  
Old 04-24-2008
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,019
use 'awk' instead of 'nawk'. (if you have 'gawk' - use that).
Try it first.
Reply With Quote
  #6  
Old 04-24-2008
Registered User
 

Join Date: Mar 2008
Posts: 30
Quote:
Originally Posted by vgersh99 View Post
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)
Reply With Quote
  #7  
Old 04-24-2008
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,019
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:13 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0