Rename the files with .xls file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename the files with .xls file
# 1  
Old 07-20-2011
Rename the files with .xls file

Hi
I need to reanme the multiple file using unix script
I have multiple file
like:
201005_SAMPLE_20110512.xls
201005_NAME_20110512.xls
201005_TEST_20110512.xls
201005_FIRST_LAST_20110512.xls
I need to rename this file
SAMPLE.xls
NAME.xls
TEST.xls
FIRSTLAST.xls
can you guide me the script.
thanks
# 2  
Old 07-20-2011
Code:
#!/usr/bin/ksh  # or bash or dash or ...
for f  in  [0-9]*.xls
do
        f2=$( echo "$f" | tr -d "[0-9]_" )
        [ -f "$f2" ] && echo "$f2 exist" && continue
        [ -d "$f2" ] && echo "$f2 dir exist" && continue
        mv "$f" "$f2"
done

# 3  
Old 07-20-2011
Hi kshji,

I have tested the above script

I have one problem

Suppose i have recived file like

201005_FIRST_LAST_20110512.xls

I need to populate target like
FIRST_LAST.xls

The above scripting is failing this file.

can you please updat me the script

Last edited by murari83.ds; 07-20-2011 at 01:58 PM..
# 4  
Old 07-20-2011
My script suggestion:

Code:
#! /bin/bash
# clean.sh

for file in *.xls; do
    filename=${file%.*}
    file_clean=`echo $filename | tr -d "[:digit:]_" `
    final="$file_clean.xls"
   mv "$file" "$final" 
   echo $final

done

# 5  
Old 07-20-2011
Hi kshji,

sorry for the confusion

I have tested the above script

I have one problem

Suppose i have recived file like

201005_FIRST_LAST_20110512.xls

I need to populate target like
FIRST_LAST.xls

The above scripting is failing this file.

can you please updat me the script
# 6  
Old 07-20-2011
So after 1st defination you found mistake in your defination. Here is solution for new defination.
Code:
#!/usr/bin/ksh  # or bash or dash or ...
for f  in  [0-9]*.xls
do
         notfirst=${f#*_}
         notlast=${notfirst%_*}
         f2=$notlast.xls
         [ -f "$f2 ] && echo "$f2 exist" && continue
         [ -d "$f2 ] && echo "$f2 dir exist" && continue
         echo "mv $f => $f2"
         mv "$f" "$f2"
done

# 7  
Old 07-20-2011
Code:
notfirst=${f#*_} notlast=${notfirst%_*}

What the special characters (i.e #, *,%,_) exactly do in the above expressions ??

Last edited by Amaravathi; 07-20-2011 at 11:41 PM.. Reason: The question need to be rephrased .
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename files based on name in text file

Hello, I have a text file "file.list" with the contents below. file1 filename1 file2 filename2 file3 filename3 file1, file2 and file3 are files existing in the same directory as the text file file.list. I want to rename file1 to filename1, file2 to filename2, as show in the text... (1 Reply)
Discussion started by: james2009
1 Replies

2. Shell Programming and Scripting

Merging Multiple XLS into Different tabs in xls/ xlsx

HI, I have multiple files per dept in folder for eg : In a folder File1_Dept100.xls File2_Dept100.xls File3_Dept100.xls File1_Dept200.xls File2_Dept200.xls File3_Dept200.xls Output should be : Dept100.xls which has File1, File2, File3 in different tabs Dept200.xls which has... (1 Reply)
Discussion started by: venkyzrocks
1 Replies

3. Shell Programming and Scripting

Perl script to Convert XLSX or XLS files to CSV file

Hi All, I've got in a situation where I need to convert .xlsx or .xls formatted files into simple text file or .csv file. I've found many options but doing this using PERL script is the best way I believe.I'm in AIX box. Perl code should have 2 params while running. i.e perl... (1 Reply)
Discussion started by: manab86
1 Replies

4. Shell Programming and Scripting

Rename files in ZIP file

Hi guys, I want to change the name of zip file and the files in zip. eg. there are two zip files 100_ABC_20101020.zip 101_ABC_20101020.zip 100_ABC_20101020.zip | --100_A_20101020.txt --100_B_20101020.txt --100_C_20101020.txt 101_ABC_20101021.zip | ... (0 Replies)
Discussion started by: ambious
0 Replies

5. Shell Programming and Scripting

Rename files that are inside zip file

Hello to all, I have a zip file with any name like FileName.zip, within the zip file there are more than 30 files with different extensions in the following format. FileName_BMN_ROSJ.txt FileName_THEUS.jpg . . . FileName_KWPWP.shx I would like to unzip the file and rename each file... (2 Replies)
Discussion started by: Ophiuchus
2 Replies

6. Shell Programming and Scripting

Rename files with tag in a .xml file

Hi Guys! 1st of all, i am sorry for my bad english! I got a Sat-Receiver and i recorded some Stuff for my Children, now the Harddrive is full and i want to convert and rename it, that i can put it on DVDs But I've got a big Problem! I want to rename the files , with a tag in a xml file... (4 Replies)
Discussion started by: crusty
4 Replies

7. Shell Programming and Scripting

Script to rename zip-files with name of file

Hi, I'm desperately in search for a solution of the following problem: I have a directory full of zip-files. All these zip-files contain a single file with a name that should be used for the name of the zip-container. Anybody a good idea. I'm an absolute beginner in shell scripting - so please... (7 Replies)
Discussion started by: mark_a17
7 Replies

8. Shell Programming and Scripting

converting xls file to txt file and xls to csv

I need to convert an excel file into a text file and an excel file into a CSV file.. any code to do that is appreciated thanks (6 Replies)
Discussion started by: bandar007
6 Replies

9. Shell Programming and Scripting

How to Merge Two .xls files side by side into a single .xls

Hi all, please help me out in below requirement. I have two .xls files say abc.xls and bbc.xls , i want to merge these two files into a single file ..say xyz.xls side by side Ex: abc.xls bbc.xls Eno Ename Eno Ename 101 hello ... (2 Replies)
Discussion started by: jagadish99
2 Replies

10. Shell Programming and Scripting

How to Merge Two .xls files side by side into a single .xls

Hi all, please help me out in below requirement. I have two .xls files say abc.xls and bbc.xls , i want to merge these two files into a single file ..say xyz.xls side by side Ex: abc.xls Eno Ename 101 hello 102 hero 103 india bbc.xls Eno Ename 201 delhi 202 Hyd 203... (0 Replies)
Discussion started by: jagadish99
0 Replies
Login or Register to Ask a Question