Unix Script for Flat File Rename

Sun microsystems Unix essentials featuri...
January 21, 2010 at 08:53:56
Specs: Windows XP
Hello,

On a daily basis, I have two .csv flat files in an FTP directory (sun solaris box) that I must manually rename. I need help creating a unix script to truncate the file name and remove the "_mmddyy" portion. Details are as follows:

File directory on FTP server: servername\export\home\userID\data

The two files in that directory that must be renamed:
Data_Withdrawals_mmddyy.csv
Data_Trx_mmddyy.csv

As I am a unix novice, is there a simple script that can remove the _mmddyy portion from each of the .csv files, giving me the following result?
Data_Withdrawals_mmddyy.csv -> Data_Withdrawals.csv
and
Data_Trx_mmddyy.csv -> Data_Trx.csv

I'd appreciate any help I can get here, thanks.


See More: Unix Script for Flat File Rename

Report •

#1
January 21, 2010 at 21:14:16
#!/bin/ksh

# I'll do the hard part - getting the new file name
# Use the Korn shell parameter substitution to trim the date
# Data_Withdrawals_mmddyy.csv -> Data_Withdrawals.csv

filename="Data_Withdrawals_mmddyy.csv"

# get the extension
ext=${filename#*.}
echo "ext is: $ext"

# get the basename
basename=${filename%%.*}
echo "$basename"

trim_the_date=${basename%_*}
echo "$trim_the_date"

# put the filename back together
newfile="${trim_the_date}.${ext}"
echo "$newfile"

# let me know if you have any questions


Report •
Related Solutions


Ask Question