Script Rename files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script Rename files
# 1  
Old 06-14-2010
Script Rename files

Hello,
I have this problem.
In a directory I have 4 csv files with this format:
Code:
PHOENIX_KM_INTERAZIONI_YYYYMMDD.csv
PHOENIX_KM_TRIPLETTE_YYYYMMDD.csv
NEWCAB_KM_INTERAZIONI_YYYYMMDD.csv
NEWCAB_KM_INTERAZIONI_YYYY_MM_DD.csv

YYYYMMDD: format CURRENT date

I wont rename all files in this format:
Code:
PHOENIX_KM_INTERAZIONI.csv
PHOENIX_KM_TRIPLETTE.csv
NEWCAB_KM_INTERAZIONI.csv
NEWCAB_KM_INTERAZIONI.csv

The script must rename all files, truncate "_YYYYMMDD" EVERY DAY.

Anyone can help me, please?

Thanks a lot

Andrew

Moderator's Comments:
Mod Comment Use code tags please, ty.

Last edited by zaxxon; 06-14-2010 at 01:22 PM.. Reason: code tags
# 2  
Old 06-14-2010
Code:
ls | cat | awk '{f=$0;sub("_[^_]+$",".csv");system("mv " f " " $0)}'

# 3  
Old 06-14-2010
Quote:
Originally Posted by bartus11
Code:
ls | cat | awk '{f=$0;sub("_[^_]+$",".csv");system("mv " f " " $0)}'

I don't usually point out UUoC instances, but that one is quite egregious Smilie

A cheaper version of your code:
Code:
for f in *_*.csv; do mv "$f" "${f%_*}.csv"; done

However, I'm not sure how NEWCAB_KM_INTERAZIONI_YYYYMMDD.csv and NEWCAB_KM_INTERAZIONI_YYYY_MM_DD.csv should be handled. If the date portion of the filename is properly stripped from both, the resulting filenames would be identical and the second file processed would clobber the first. Our proposed solutions only strip the final/only date segment, so there is no clobbering. How this should be handled if this result isn't good enough should be clarified by the original poster.

Regards,
Alister
# 4  
Old 06-14-2010
Code:
#!/bin/bash
i=1
 ls -1 *.csv > list
while read file
  do
newfile=$(sed -e 's/\(.*\)\(_YY......\).*/\1/' -e $i\ '!d' list).csv
mv $file $newfile
((i++))
done < list
rm -f list

# 5  
Old 06-14-2010
Code:
$ ls *KM*[0-9][0-9].csv | while read line; do
new=$(echo "$line" | sed 's/_[0-9]\{4\}_\{0,1\}[0-9][0-9]_\{0,1\}[0-9][0-9]//')
mv -v "$line" "$new"
done
NEWCAB_KM_INTERAZIONI_22221130.csv -> NEWCAB_KM_INTERAZIONI.csv
NEWCAB_KM_INTERAZIONI_2222_11_30.csv -> NEWCAB_KM_INTERAZIONI.csv
PHOENIX_KM_INTERAZIONI_22221130.csv -> PHOENIX_KM_INTERAZIONI.csv
PHOENIX_KM_TRIPLETTE_22221130.csv -> PHOENIX_KM_TRIPLETTE.csv
$



---------- Post updated at 21:01 ---------- Previous update was at 20:56 ----------

Okay, now I see why this is no option Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script for rename many files

Hello friends! I have a problem with my script. I'm a italian boy. Sorry for my english ehehehehehhe. I've many files .jpg and I would like rename they in this mode: I have not files with progressive number e I would like rename with progressive number. Example: DSC_0012.JPG DSC_0582.JPG... (7 Replies)
Discussion started by: vegetablu
7 Replies

2. Shell Programming and Scripting

Script to unzip files and Rename the Output-files

Hi all, I have a many folders with zipped files in them. The zipped files are txt files from different folders. The txt files have the same names. If i try to find . -type f -name "*.zip" -exec cp -R {} /myhome/ZIP \; it fails since the ZIP files from different folders have the same names and... (2 Replies)
Discussion started by: pmkenya
2 Replies

3. Shell Programming and Scripting

script to rename mp3 files

hi there, i'm using OS X. i have a bunch of mp3 files strewn across a directory tree that i'd like to rename. specifically i'd like to remove any track numbers and leading non-alphabetic characters from the filenames like this: 01 - song1.mp3 2 song2.mp3 become: song1.mp3... (6 Replies)
Discussion started by: creakyshrimp
6 Replies

4. Shell Programming and Scripting

Rename files in the script

Hi All, I want to write a script to rename the file in to the incremental order for example Original file filename=/nfs/n1/file1.img filename=/nfs/n1/file1.img filename=/nfs/n1/file1.img filename=/nfs/n1/file1.img filename=/nfs/n1/file1.img I want output shpuld be... (4 Replies)
Discussion started by: mangeshpardhi
4 Replies

5. Shell Programming and Scripting

Script to rename files

I have the following directories in my home directory, my scripts dbmig es ms_done my-home I want my output to look like the following MyScripts DbmigEs MsDone MyHome Basically, I want to get rid of spaces,special characters and convert the first letter of each word to uppercase and... (1 Reply)
Discussion started by: ramky79
1 Replies

6. UNIX for Dummies Questions & Answers

Script to Rename Files

I wrote a simple script that converts my windows text files to unix, so that I can compare them to different unix files purposes of my project. win2unix file1.txt file1Win.txt win2unix file2.txt file2Win.txt etc Is there a way to simplify this to: <while .txt in... (5 Replies)
Discussion started by: idano530
5 Replies

7. Shell Programming and Scripting

Shell Script to rename files

Hi, i need a bit of help writting a tcsh script which renames all ascii text files in the current directory by adding a number to their names before the extension so for example, a directory containing the files Hello.txt Hello.t Hello should have the following changes, Hello.txt... (2 Replies)
Discussion started by: yakuzaa
2 Replies

8. Shell Programming and Scripting

Script to rename files

Let me preface this by stating I have absolutely no idea what I'm doing in this arena, but I'm in need of a little help here. I need to take filenames like this: amwed_0402c-slug~1-cp.jpg And reduce them to slug~1.jpg That is, I need to remove the first 12 and last 3 characters. The... (3 Replies)
Discussion started by: cpreovol
3 Replies

9. UNIX for Dummies Questions & Answers

Script to rename files

Have files of the sort 3p1522015.dgn and need to have them renamed to 152201.dgn. Essentially dropping the 1st 2 characters and the last. I'm relatively new to UNIX and uncertain of where to start. I hope this provides enough detail. Thanks (5 Replies)
Discussion started by: Dinkster
5 Replies

10. OS X (Apple)

Rename Files with a script ?

Hi All !!! Is there any solution to get rid of / " * in old files names WITH A SCRIPT (About 100 Gb of old files) I know it can be done i just dont know how ! Hope that some one can help Best R. Yovel (1 Reply)
Discussion started by: yoveln
1 Replies
Login or Register to Ask a Question