Renaming files in one file from names in other


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Renaming files in one file from names in other
# 1  
Old 03-11-2011
Renaming files in one file from names in other

Hi Guys,
I have a small problem of renaming multiple files. For example I have names of a set of files in one directory like
Code:
K2_34625-34675
K7_988963-988983
K12_773882-7734102

and the other set corresponding to the same is
Code:
U_P_321_9_3_11.ab1
U_P_322_9_3_11.ab1
U_P_323_9_3_11.ab1

Now what I would like is that the new renamed file looks like this so that it computes the mean of the last two values in first set ; for example
K2_34625-34675 should become K2_34650 and so on for the other names in the first set,
then these new names have to be used to rename the second set of files for example ;
U_P_321_9_3_11.ab1 should become K2_36450.ab1, and so on.

Could anyone please help out on this as I have a long list like this.

Hope to get some answers

Thanx in advance

Pawan
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!


---------- Post updated 03-11-11 at 04:30 AM ---------- Previous update was 03-10-11 at 04:02 PM ----------

Thank you moderator for correcting my input .... I was not aware of such a thing as code tag.

Hope now the input is OK for a reply from the forum.

Looking forward to it .... Thanks in advance Smilie

Last edited by vgersh99; 03-10-2011 at 05:05 PM.. Reason: code tags, please!
# 2  
Old 03-12-2011
Help with controlling string elements

Hi All

Has anyone had a few moments to look at my previous query,

I'll really appreciate feedback of any sort to help me start experimenting myself,

I have a general difficulty in understanding how to control single elements within a string. An example,

Code:
XYZ1234      ABCD5678

My expected output is :

Code:
ABCD1234     XYZ5678  (swapping subset of string elements of choice)
XYZ37            ACBD1214 (making calculations using string elements)

,etc

Could someone illuminate on such problems please

Thanks in advance Smilie

PS: I'm a beginner to UNIX
# 3  
Old 03-12-2011
Code:
[ctsgnb@shell ~/sand]$ cat f1
K2_34625-34675
K7_988963-988983
K12_773882-7734102
[ctsgnb@shell ~/sand]$ cat f2
U_P_321_9_3_11.ab1
U_P_322_9_3_11.ab1
U_P_323_9_3_11.ab1
[ctsgnb@shell ~/sand]$ paste f2 f1 | while read a b
> do
> t=${b##*_???}
> v=$(echo $b | sed 's/.*_...//;s/-.../-/')
> n=${b%$t}$(( -($v) )).${a#*.}
> echo "mv $a $n"
> done
mv U_P_321_9_3_11.ab1 K2_34650.ab1
mv U_P_322_9_3_11.ab1 K7_98820.ab1
mv U_P_323_9_3_11.ab1 K12_7733220.ab1
[ctsgnb@shell ~/sand]$

If it does what you want then just change the echo "mv $a $n" with mv "$a $n"

---------- Post updated at 12:42 AM ---------- Previous update was at 12:34 AM ----------

please give more clue about the caculation for the renaming :

34625-34675 = -50
and what should be calculated in the
773882-7734102
????

I assumed you want to keep the first 3 number unchanged so that is becomes:
-(882-4102)=3220
so the new name will be K12_7733220.ab1
by the way, i also assume you have the same number of entries in your both set of files
This User Gave Thanks to ctsgnb For This Post:
# 4  
Old 03-13-2011
Thanks CTSGNB,
Your suggestion is producing the exact output as you mentioned, but my expected outcome for input is as follows
Code:
input
K2_34625-34675
U_P_321_9_3_11.ab1

output before calculation
K2_34625-34675.ab1
For the calculation I want to calculate the mean of the two numbers separated by "-" which does not imply subtraction. Thus my final expected output for each entry in both set of files should be for example:
K2_34650.ab1
Please not the one set of entries are just assigned names eg. K2_34625-34675 upon which the calculation need to be done and then assigned the modified name to another entry (those ending with .ab1) which are real data containing files to be renamed.

I hope I'm clear enough.

Thank you for looking into this.

Have a nice sunday.
# 5  
Old 03-13-2011
ok you mean the "average" of these numbers ?
so give a try to
Code:
paste f2 f1 | while read a b
do
t=${b##*_}
v=$(echo $b | sed 's/.*_//;s/-/+/')
n=${b%$t}$(( ($v) / 2 )).${a#*.}
echo "mv $a $n"
done

This should give you something like :

Code:
mv U_P_321_9_3_11.ab1 K2_34650.ab1
mv U_P_322_9_3_11.ab1 K7_988973.ab1
mv U_P_323_9_3_11.ab1 K12_4253992.ab1

if it fits your needs, just change
Code:
echo "mv $a $n"

with
Code:
mv "$a $n"

# 6  
Old 03-13-2011
Hi there,

I tried your code but its giving me the following output:

Code:
:~ noel$ paste b.txt a.txt | while read a b
> do
> t=${b##*_}
> v=$(echo $b | sed 's/.*_//;s/-/+/')
> n=${b%$t}$(( ($v) / 2 )).${a#*.}
> mv "$a $n"
> done 
usage: mv [-f | -i | -n] [-v] source target
       mv [-f | -i | -n] [-v] source ... directory
usage: mv [-f | -i | -n] [-v] source target
       mv [-f | -i | -n] [-v] source ... directory
usage: mv [-f | -i | -n] [-v] source target
       mv [-f | -i | -n] [-v] source ... directory
-bash: () / 2 : syntax error: operand expected (error token is ") / 2 ")

but when I remove the "" in the mv command I get

Code:
> mv $a $n> done
mv: rename U_P_321_9_3_11.ab1 to K2_34650.ab1: No such file or directory
mv: rename U_P_322_9_3_11.ab1 to K7_988973.ab1: No such file or directory
mv: rename U_P_323_9_3_11.ab1 to K12_4253992.ab1: No such file or directory
-bash: () / 2 : syntax error: operand expected (error token is ") / 2 ")

Is this because a.txt and b.txt in this case are files containing entries and not real .ab1 files ??

I will do this will real files soons and let you know but if you can comment on the code to simplfy what its doing its will be a big help. At the moment I dont understand it

Thanks and have a nice evening. Smilie
# 7  
Old 03-13-2011
Try to run it using korn shell (ksh).

1) Are all your U_P*.ab1 files located in the same directory ? if so, in which one ?
2) Are all your K*-* files located in the same directory ? if so, in which one ?
3) This link should help understanding the code of pattern matching operator : String Operators (Learning the Korn Shell, 2nd Edition) especially Table 4-9.
For the $(( ... )) notation, you can read Numeric Variables and Arithmetic (Learning the Korn Shell, 2nd Edition)
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Renaming the file names in a directory

Hi, I have about 60 files in a directory and need to rename those files. For example the file names are i_can_phone_yymmdd.txt (where yymmdd is the date. i.e 170420 etc) i_usa_phone_1_yymmdd.txt i_eng_phone_4_yymmdd.txt The new file names should be phone.txt phone_1.txt phone_4.txt I am... (4 Replies)
Discussion started by: naveed
4 Replies

2. Shell Programming and Scripting

Exclude certain file names while selectingData files coming in different names in a file name called

Data files coming in different names in a file name called process.txt. 1. shipments_yyyymmdd.gz 2 Order_yyyymmdd.gz 3. Invoice_yyyymmdd.gz 4. globalorder_yyyymmdd.gz The process needs to discard all the below files and only process two of the 4 file names available ... (1 Reply)
Discussion started by: dsravanam
1 Replies

3. Shell Programming and Scripting

Renaming File Names in a folder/Dir

Hi Team, I'm new to Unix shell scripting . I've the following requirement A folder contains the list of files with the following format ab.name.11.first ab.name.12.second ab.name.13.third ---------- I have to rename the above file to like below ... (6 Replies)
Discussion started by: smile689
6 Replies

4. UNIX for Dummies Questions & Answers

Renaming files with weird names

I have hundreds of files with weird names, something like this: I was wondering how can I rename them all keeping the sampleid and the last extension, something like this: Any help will be greatly appreciated. (5 Replies)
Discussion started by: Xterra
5 Replies

5. Shell Programming and Scripting

Shell Scripts (Renaming file names with sequential numbers)

Hi there, Firstly, I have no experience with shell scripts so would really appreciate some help. I have the following shell script that is causing some problems: moveit() { && set -x if then DOUBLE_DELIVERY=$(grep... (6 Replies)
Discussion started by: thebeno
6 Replies

6. Shell Programming and Scripting

Renaming file names

I have 7 files with 7 different names coming into a specified folder on weekly basis, i need to pick a file one after another and load into oracle table using sql loader. I am using ksh to do this. So in the process if the file has error records and if sql loader fails to load into oracle tables,... (2 Replies)
Discussion started by: vpv0002
2 Replies

7. Shell Programming and Scripting

renaming files from an array of names

I haven’t used Unix in over 25 years … and so I am at a loss for something that should be very simple. I have a lot of jpeg files (i.jpg) of students in a yearbook.. I also have an array name(i) of their names. I need to rename each “i.jpg” to “name(i).jpg”. I believe the ksh script... (11 Replies)
Discussion started by: chuckmg
11 Replies

8. Shell Programming and Scripting

File renaming from list of names contained in another file

I have to rename a large number of files so that the name of each file corresponds to a code number that is given side by side in a list (textfile). The list contains in column A the filename of the actual files to be renamed and in column B the name (a client code, 9 digits) that has to be... (7 Replies)
Discussion started by: netfreighter
7 Replies

9. UNIX for Dummies Questions & Answers

Some questions - renaming duplicate names

I have a file that looks like this 2 4 10 500 tim9 5 8 14 700 tim9 3 5 15 432 john1 1 4 12 999 ellen2 So basically what i want to do is fine duplicate names on column 5 and rename it with an extention (i.e. tim9_1 and tim9_2). so the output file will look like this 2 4 10 500 tim9_1... (1 Reply)
Discussion started by: kylle345
1 Replies

10. Shell Programming and Scripting

Renaming file names in a shell script

I want to write a shell script that will rename all the file names to today's date attached to it.. so for example i have a file names like file1.sales.20081201.txt.c zbrs.salestxtn.20091101.txt.inn then it will rename both the files with todays date to it so the file names get changed... (1 Reply)
Discussion started by: rudoraj
1 Replies
Login or Register to Ask a Question