Renaming files based on data in a spreadsheet


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Renaming files based on data in a spreadsheet
# 1  
Old 06-21-2010
Renaming files based on data in a spreadsheet

I have a spreadsheet in a folder that looks like this:
 
Sector1...Sector2...Sector3...Sector4...Sector5...Sector6...Sector7
SomeID....Title.......SomeID...SomeID....SomeID...SomeID....SomeID
OtherID...MyTitle....SomeID...SomeID....SomeID...SomeID....SomeID
ID..........YourTitle..SomeID...SomeID....SomeID...SomeID....SomeID
In the same folder I have all necessary folders named as Sector1, Sector2, etc. What I need is to use the first ID listed under Sector1 (SomeID) in the sreadsheet and rename the first file (XXXXX1.JPG) located in folder Sector1 to SomeID. Then go to the second ID (OtherID) and rename the file (XXXXX2.JPG) as OtherID, so on and so forth till all the IDs on that column have been use. After that, go to the next column (Sector2) and rename the first file (xxxx1.jpg) Title, then the second, etc.
Can this be done?
# 2  
Old 06-21-2010
Hi, you could export the spreadsheet as tab delimited ascii file and do something like this
Code:
awk 'NR==1{split($0,S);next}{for (i=1; i<=NF; i++) print "mv",S[i]"/XXXXX"(NR-1),S[i]"/"$i}' infile | sh

# 3  
Old 06-22-2010
Scrutinizer

It is almost what I need but when I wrote "XXXXX" I was trying to say that the files in the subfolders could have any name such as "Jan 2010 Ana1.jpg" ,"Alberto2.pdf" or "Farewell lunch3.tif". Moreover, if the files have any extension (as .JPG, .PDF or .TIF like in in my example ), the script would not recognize the files and I would get the following output:
Quote:
mv: cannot stat `Sector1/XXXXX1': No such file or directory
mv: cannot stat `Sector2/XXXXX1': No such file or directory
mv: cannot stat `Sector3/XXXXX1': No such file or directory
mv: cannot stat `Sector1/XXXXX2': No such file or directory
mv: cannot stat `Sector2/XXXXX2': No such file or directory
mv: cannot stat `Sector3/XXXXX2': No such file or directory
mv: cannot stat `Sector1/XXXXX3': No such file or directory
mv: cannot stat `Sector2/XXXXX3': No such file or directory
mv: cannot stat `Sector3/XXXXX3': No such file or directory
If I rename the files to XXXXX1, XXXXX2 and XXXXX3 then everything is fine.
How can I modify your script in such way that it can recognize any file name followed by a number (1,2,3,4,5,6,7,8,9,.....), no matter what extension is present (Party1.jpg, Presentation for tomorrow2.ppt, 454 data3.pdf)
Thanks once again!

Last edited by Xterra; 06-22-2010 at 01:07 AM..
# 4  
Old 06-22-2010
Code:
awk 'NR==1{split($0,S);next}{for (i=1; i<=NF; i++) print "mv",S[i]"/*"(NR-1),S[i]"/"$i}' infile | sh

# 5  
Old 06-22-2010
Extension

Scrutinizer,

It is great! The only problem is the extension. For instance, imagine that the files I have in folder "Sector1" are the following ones:

A1.doc
B2.doc
C3.doc

If I use your code I get the following:

Quote:
mv: cannot stat `Sector1/*1': No such file or directory
mv: cannot stat `Sector1/*2': No such file or directory
mv: cannot stat `Sector1/*3': No such file or directory
If I remove the extension

A1
B2
C3

Then everything is great. Would it be possible to modify the script in such way that the file extension is ignore?
# 6  
Old 06-24-2010
Example

Scrutinizer,

I have uploaded the zip file containing all folders and files which I described in my message.
Thanks once again for all your help,

Xterra
# 7  
Old 06-24-2010
Hi Xterra,

One problem is in the first word 'patiente ' in Libro.txt on line 2 and further. This makes the file differ from your original specification. I removed it and then is seemed to work properly.

The other problem is that with the awk code above there is a problem when there is a file called 1 and a file call 11 for example. The * wildcard catches both which is not as intended.

This can be remedied by dropping the * :
Code:
awk 'NR==1{split($0,S);next}{for (i=1; i<=NF; i++) print "mv",S[i]"/"(NR-1),S[i]"/"$i}' infile | sh

S.

Last edited by Scrutinizer; 06-24-2010 at 04:19 AM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get information from one files, based on data from other file

Hello. I am trying to get some info from log file. I have fileA , which contains all the country prefixes (the file contains one column and "n" rows ). And i have fileB, which contains huge data of phone numbers (the file contains one column and "n" rows). What i want to do is, to count... (7 Replies)
Discussion started by: dragonfly85
7 Replies

2. Shell Programming and Scripting

In PErl script: need to read the data one file and generate multiple files based on the data

We have the data looks like below in a log file. I want to generat files based on the string between two hash(#) symbol like below Source: #ext1#test1.tale2 drop #ext1#test11.tale21 drop #ext1#test123.tale21 drop #ext2#test1.tale21 drop #ext2#test12.tale21 drop #ext3#test11.tale21 drop... (5 Replies)
Discussion started by: Sanjeev G
5 Replies

3. UNIX for Beginners Questions & Answers

Create file based on data from two other files

I have looked through several threads regarding merging files with awk and attempted using join however have been unsuccessful likely as I do not fully understand awk. What I am attempting is to take a csv file which could be between 1 and 15,000 lines with 5 colums and another csv file that will... (4 Replies)
Discussion started by: cdubu2
4 Replies

4. Shell Programming and Scripting

Help on writing data from 2 different files to one based on a common factor

Hello all, I have 2 text files. For example: File1.txt contains data A B C D ****NEXT**** X Y Z ****NEXT**** L M N and File2.txt contains data (13 Replies)
Discussion started by: vat1kor
13 Replies

5. Shell Programming and Scripting

Help me pls : splitting single file in unix into different files based on data

I have a file in unix with sample data as follows : -------------------------------------------------------------- -------------------------------------------------------------- {30001002|XXparameter|Layout|$ I want this file to be splitted into different files and corresponding to the sample... (54 Replies)
Discussion started by: Ravindra Swan
54 Replies

6. Shell Programming and Scripting

Reading 2 CSV files and filtering data based on group

I have two CSV files in the following format: First file: GroupID, PID:TID, IP, Port Sample data: 0,1000:11,127.0.0.1,445 0,-1:-1,127.0.0.1,800 1,1000:11,127.0.0.1,445 1,-1:-1,127.0.0.1,900 2,1000:11,127.0.0.1,445 2,-1:-1,180.0.0.3,900 Second file: IP,Port,PID Sample data... (6 Replies)
Discussion started by: rakesh_arxmind
6 Replies

7. Shell Programming and Scripting

data for spreadsheet

I'm pulling down some LUN usage data once per day. I store the data in a file name that matches the name of the LUN. Then I just append new usage amounts to the same file each day. Filename might be serv01_luna, serv01_lunb, serv01_lunc, etc, etc. Inside the file it would like the following... (7 Replies)
Discussion started by: dwcasey
7 Replies

8. Shell Programming and Scripting

generate report based on data in files.

Hi All, I need to develop a shell script which does sanity check of a data file, as below. 1. For DATE columns, it should check if date is given in proper format or not? For example, if format of date is expected as DD-MON-YYYY HH24:MI:SS and we received the date in formation like DDMMYYYY HH24,... (1 Reply)
Discussion started by: ace_friends22
1 Replies

9. Shell Programming and Scripting

Renaming Files Based on Contents

Hello everyone, I currently have a situation which is causing me some issues for keeping up with certain files. I will explain this to the best of my abilities. I have a list of files as follows 50_REPORT_1111 - file contains the word Car 50_REPORT_2222 - file contains the word House... (15 Replies)
Discussion started by: DerangedNick
15 Replies

10. Shell Programming and Scripting

Converting HTML data into a spreadsheet

Hi, I have a perl script that prints some data in the form of a table (HTML table) Now, I want to be able to convert this data into a report on an Excel sheet. How can I do this? Regards, Garric (4 Replies)
Discussion started by: garric
4 Replies
Login or Register to Ask a Question