![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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. |
|
#3
|
||||
|
||||
|
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
|
|||
|
|||
|
Quote:
./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
|
||||
|
||||
|
use 'awk' instead of 'nawk'. (if you have 'gawk' - use that).
Try it first. |
|
#6
|
|||
|
|||
|
Quote:
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
|
||||
|
||||
|
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)
}'
Let's make sure the 'mv' commands are correct. After we're satisfied with that: Code:
./testScript.sh | sh |
||||
| Google The UNIX and Linux Forums |