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



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

Report •

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:


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 •

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

Report •

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

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 •

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
# place script here, str var holds the file name

Report •

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

you've helped me out before

always great


Report •

Ask Question