need script to reflect date in new file name

February 9, 2011 at 12:43:43
Specs: Windows XP
How would I rename this file in the folowing format

newname.yyyymmddhhmm

FIL.92.92.92.92.922-Ports8-FIL.OHs.tested01fil_tes.2011.02.06_00.45.01_ADT.csv


See More: need script to reflect date in new file name

Report •

#1
February 9, 2011 at 16:43:53
Assuming your file structure doesn't change and awk changing the field delimiter to a period, the 5th field from the end is the year, the 4th field is the month, the 3rd field is the hour and minute provided the under score is removed. Should work with bash or ksh:


str="FIL.92.92.92.92.922-Ports8-FIL.OHs.tested01fil_tes.2011.02.06_00.45.01_ADT.csv"

newdate=$(echo "$str"|awk ' BEGIN { FS="." }
{
# get rid of the underscore
gsub("_", "", $(NF-3) )
printf("%s%s%s", $(NF-5), $(NF-4), $(NF-3) )

} ')

# change cp to mv when ready
cp "$str" "newname.${newdate}"


Report •

#2
February 10, 2011 at 04:25:05
Thanks for your help nails

I am getting the following error though

awk: syntax error near line 4
awk: illegal statement near line 4


I am getting the first part of the new file
newname.


Report •

#3
February 10, 2011 at 08:07:41
It's probably an environment issue. You are running awk on what OS?

If you are running Solaris Unix, try nawk.


Report •

Related Solutions

#4
February 10, 2011 at 09:00:10
that worked great nails..changed awk to nawk

how would I run this against a bunch of files with the same structure but different timestamps on each file.

need a new file name for each file


Report •

#5
February 10, 2011 at 09:14:23
One way is to place the script inside a for loop:

# cd <to your directory>
for str in *.csv
do
# place script here, str var holds the file name
done


Report •

#6
February 10, 2011 at 09:21:41
wow...worked great...I really appreciate your help

you've helped me out before

always great

Thanks


Report •

Ask Question