I need a script that will raname the following file names that beging with 08078* in unix as follows:
Rename 08078-08201103-H00044-CA.835
as follows:
08078-110820-H000440CA.835
Bascially it will do this:
1) Keep the first 6 positons.
2) Move the yr from the file name to be the first after the first hyphen
to be like this 110820 "yymmdd"
3) Remove the 03 after 11 in the file name so the final file name is 08078-110820-H00044.CA.835
Please let me know as soon as possible. I am pressed for time and hope to have an anser to today if possible. Thank you
I figured it out on my own after I posted this. It is working. Here is how I did it:
I have files coming like this: 08101101.wsp ...etc I finally got it to work to write out the file per specs as:
08078-110810-H00044-C01W.835
and her is the code I came up with and it works:
GetFileName=08
#First loop to add 08078 at the begining of the file and and H00044-C.835 # at the end:
for file in $GetFileName*.*; do
newfile=`echo 08078-$file-H00044-C.835`
mv $file ./$newfile
done
# 2nd For loop to only keep one extention on the file name
for file in 08078-*.835; do
newfile=`echo $file | awk -F'.' 'BEGIN{OFS=""}{s=$NF;o=$0;$NF="";print "mv "o" " $0"."s}'|sh`
done
# 3rd For loop to re-arrange how to rename the file:
for file in 08078-*.835; do
GetNumb=`echo $file | cut -c1-5`
Getmmdd=`echo $file | cut -c7-10`
GetYear=`echo $file | cut -c11-12`
GetSeq=`echo $file | cut -c13-15`
GetIns=`echo $file | cut -c19-24`
GetChar=`echo $file | cut -c26-26`
GetRest=`echo $file | cut -c 27-`
NewFile=$GetNumb-$GetYear$Getmmdd-$GetIns-$GetChar$GetSeq$GetRest
mv $file ./$NewFile
done
It works for what I want it to do. Is there a better way to do this "Maybe with one for loop vs. 3 loops ?
#! /usr/bin/ksh
###############
ls | grep "^08078" | while read org_name ; do
print "Org name : $org_name"
##
## Split filename in separate parts
##
tmp=$org_name
pre=${tmp%%-*}
tmp=${tmp#*-}
date=${tmp%%-*}
tmp=${tmp#*-}
string=${tmp%%-*}
tmp=${tmp#*-}
code=${tmp%%.*}
post=${tmp##*.}
##
## Split Date in Year Month and Day
##
year=${date%??}
year=${year#????}
month=${date%??????}
day=${date#??}
day=${day%????}
##
## Define new name
##
print "New name : $pre-$year$month$day-$string$code.$post"
done
Org name : 08078-08201103-H00044-CA.835
New name : 08078-110820-H00044CA.835