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

K2_34625-34675
K7_988963-988983
K12_773882-7734102

and the other set corresponding to the same is

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

---------- 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 :slight_smile:

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,

XYZ1234      ABCD5678

My expected output is :

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 :slight_smile:

PS: I'm a beginner to UNIX

[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

Thanks CTSGNB,
Your suggestion is producing the exact output as you mentioned, but my expected outcome for input is as follows

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.

ok you mean the "average" of these numbers ?
so give a try to

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 :

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

echo "mv $a $n"

with

mv "$a $n"

Hi there,

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

:~ 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

> 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. :slight_smile:

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)

I'm using a bash shell

  1. Yes all my U_P*.ab1 files are located in the same directory.
  2. Although all my K*-* names are located in a different file.

I'm still having problems to figure this out. So to clarify, K*-* names are stored as a .txt file, and these nales have to be used to rename the U_P*.ab1 files to give rise to renamed K*-*.ab1 files.

Thank you for your help and input.

Have a nice day

---------- Post updated at 05:35 AM ---------- Previous update was at 05:33 AM ----------

Correction in previous reply :
I'm still having problems to figure this out. So to clarify, K*-* names are stored as a .txt file, and these nales (names) have to be used to rename the U_P*.ab1 files to give rise to renamed K*-*.ab1 files.

Cheers

Please give more infos :
give the full PATH of WHERE all your K* and U*.ab1 files are located as well as WHERE your .txt files are located (those which contain the lists of their K and U*.ab1 names)
and use ksh when running the code i provide.

my U*.ab1 files are located in the directory:

/Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results

my .txt file containing the K* names is located:

/Users/pawannoel/Desktop

And how do I change to ksh from bash to run your scripts?

Cheers

once renamed, do you want them to be in

/Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results ?

or in

/Users/pawannoel/Desktop ?

I want to rename them in

/Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results

Please also provide what output you have when running the following command :

uname -a

---------- Post updated at 12:54 PM ---------- Previous update was at 12:53 PM ----------

Ok then try this :

cd /Users/pawannoel/Desktop
ksh
MYDIR=/Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results
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 "$MYDIR/$a $MYDIR/$n"
done 

---------- Post updated at 01:01 PM ---------- Previous update was at 12:54 PM ----------

You shouldn't get errors ... except if some files mentionned in *.txt does not exist in

/Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results

I'm afraid, due to data protection I cant provide you my machine ID as I am on an institutional machine, but I am getting the following results after your script

Notice that I am trying to work inside the "test" directory in the MYDIR path

u830-tro-6-35-6647:~ pawannoel$ ksh
$ MYDIR=/Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results/test
$ 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 "$MYDIR/$a $MYDIR/$n"
> done
paste: b.txt: No such file or directory
$ 

Both a.txt and b.txt are on the Desktop and look like this

a.txt :

K7_S5_279891-280489
K7_S6_279891-280489
K7_S7_279891-280489
K7_S8a_279891-280489
K7_S5_1083565-1084163
K7_S6_1083565-1084163
K7_S7_1083565-1084163
K7_S8a_1083565-1084163

b.txt:

U3244_AN_PN_11_03_11_1_A08_002.ab1
U3244_AN_PN_11_03_11_2_B08_004.ab1
U3244_AN_PN_11_03_11_3_C08_006.ab1
U3244_AN_PN_11_03_11_4_D08_008.ab1
U3244_AN_PN_11_03_11_5_E08_010.ab1
U3244_AN_PN_11_03_11_6_F08_012.ab1
U3244_AN_PN_11_03_11_7_G08_014.ab1
U3244_AN_PN_11_03_11_8_H08_016.ab1

Do a.txt and b.txt need to be in the MYDIR path?

Thanks

---------- Post updated at 07:37 AM ---------- Previous update was at 07:31 AM ----------

I have tried to run the same commands after copying both a.txt and b.txt in the MYDIR path and I get the same result as above !!

Any idea?

1) Your .txt files should be in /Users/pawannoel/Desktop
2) Your U
.ab1 should be in /Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results

3) Then run the following :

cd /Users/pawannoel/Desktop
ksh
MYDIR=/Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results
unset t v n
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 "$MYDIR/$a $MYDIR/$n"
done

---------- Post updated at 02:26 PM ---------- Previous update was at 02:19 PM ----------

On which OS are you ?

It worked partially ..... It didnt rename the first .ab1 file.

Also the "" in the mv command leads to error so I removed them and ran your commands

So the list below was before renaming

U3244_AN_PN_11_03_11_1_A08_002.ab1    U3244_AN_PN_11_03_11_2_B08_004.ab1  
U3244_AN_PN_11_03_11_3_C08_006.ab1 U3244_AN_PN_11_03_11_4_D08_008.ab1    U3244_AN_PN_11_03_11_5_E08_010.ab1
U3244_AN_PN_11_03_11_6_F08_012.ab1
U3244_AN_PN_11_03_11_7_G08_014.ab
U3244_AN_PN_11_03_11_8_H08_016.ab11

The list after renaming is

K7_S5_1083864.ab1                     
K7_S6_1083864.ab1                        
K7_S6_280190.ab1           
K7_S7_1083864.ab1 
K7_S7_280190.ab1
K7_S8a_1083864.ab1 
K7_S8a_280190.ab1
U3244_AN_PN_11_03_11_1_A08_002.ab1

I'm using OSX 10.5.8

thnx, we are nearly there with this ... appreciate your help :slight_smile:

what gives :

cd /Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results
ls | grep ' '

?
what gives

ls -F

?

ls | grep ' ' gives:

K7_S5_1083864.ab1
K7_S6_1083864.ab1
K7_S6_280190.ab1
K7_S7_1083864.ab1
K7_S7_280190.ab1
K7_S8a_1083864.ab1
K7_S8a_280190.ab1
U3244_AN_PN_11_03_11_1_A08_002.ab1

ls -F gives:

K7_S5_1083864.ab1*            K7_S6_280190.ab1*            K7_S7_280190.ab1*            K7_S8a_280190.ab1*
K7_S6_1083864.ab1*            K7_S7_1083864.ab1*            K7_S8a_1083864.ab1*            U3244_AN_PN_11_03_11_1_A08_002.ab1*

please provide :

cat /Users/pawannoel/Desktop/a.txt
cat /Users/pawannoel/Desktop/b.txt
cat b.txt | xargs -i ls /Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results/{}

run this :

ksh
cd /Users/pawannoel/Desktop
unset t v n
MYDIR=/Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results
paste ./b.txt ./a.txt | while read a b
do
[[ ! -f "$MYDIR/$a" ]] && echo "$MYDIR/$a does not exists, script abort" && break
echo "before assignment t=$t v=$v n=$n a=$a b=$b"
t=${b##*_}
v=$(echo $b | sed 's/.*_//;s/-/+/')
n=${b%$t}$(( ($v) / 2 )).${a#*.}
echo "before mv t=$t v=$v n=$n a=$a b=$b"
echo "mv $MYDIR/$a $MYDIR/$n"
mv "$MYDIR/$a" "$MYDIR/$n"
unset t v n
echo "after unset t=$t v=$v n=$n a=$a b=$b"
done

And copy paste the output you will get

u830-tro-6-35-6647:~ pawannoel$ cat /Users/pawannoel/Desktop/a.txt
K7_S5_279891-280489
K7_S6_279891-280489
K7_S7_279891-280489
K7_S8a_279891-280489
K7_S5_1083565-1084163
K7_S6_1083565-1084163
K7_S7_1083565-1084163
K7_S8a_1083565-1084163
u830-tro-6-35-6647:~ pawannoel$ cat /Users/pawannoel/Desktop/b.txt
U3244_AN_PN_11_03_11_1_A08_002.ab1
U3244_AN_PN_11_03_11_2_B08_004.ab1
U3244_AN_PN_11_03_11_3_C08_006.ab1
U3244_AN_PN_11_03_11_4_D08_008.ab1
U3244_AN_PN_11_03_11_5_E08_010.ab1
U3244_AN_PN_11_03_11_6_F08_012.ab1
U3244_AN_PN_11_03_11_7_G08_014.ab1
U3244_AN_PN_11_03_11_8_H08_016.ab1
u830-tro-6-35-6647:~ pawannoel$ cat b.txt | xargs -i ls /Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results/test/{}
cat: b.txt: No such file or directory
xargs: illegal option -- i
usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]
             [-L number] [-n number [-x]] [-P maxprocs] [-s size]
             [utility [argument ...]]

u830-tro-6-35-6647:Desktop pawannoel$ ksh
$ cd /Users/pawannoel/Desktop
$ unset t v n
$ MYDIR=/Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results/test
$ paste ./b.txt ./a.txt | while read a b
> do
> [[ ! -f "$MYDIR/$a" ]] && echo "$MYDIR/$a does not exists, script abort" && break
> echo "before assignment t=$t v=$v n=$n a=$a b=$b"
> t=${b##*_}
> v=$(echo $b | sed 's/.*_//;s/-/+/')
> n=${b%$t}$(( ($v) / 2 )).${a#*.}
> echo "before mv t=$t v=$v n=$n a=$a b=$b"
> echo "mv $MYDIR/$a $MYDIR/$n"
> mv "$MYDIR/$a" "$MYDIR/$n"
> unset t v n
> echo "after unset t=$t v=$v n=$n a=$a b=$b"
> done
before assignment t= v= n= a=U3244_AN_PN_11_03_11_1_A08_002.ab1 b=K7_S5_279891-280489
before mv t=279891-280489 v=279891+280489 n=K7_S5_280190.ab1 a=U3244_AN_PN_11_03_11_1_A08_002.ab1 b=K7_S5_279891-280489
mv /Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results/test/U3244_AN_PN_11_03_11_1_A08_002.ab1 /Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results/test/K7_S5_280190.ab1
mv: mv: cannot execute [Exec format error]
after unset t= v= n= a=U3244_AN_PN_11_03_11_1_A08_002.ab1 b=K7_S5_279891-280489
before assignment t= v= n= a=U3244_AN_PN_11_03_11_2_B08_004.ab1 b=K7_S6_279891-280489
before mv t=279891-280489 v=279891+280489 n=K7_S6_280190.ab1 a=U3244_AN_PN_11_03_11_2_B08_004.ab1 b=K7_S6_279891-280489
mv /Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results/test/U3244_AN_PN_11_03_11_2_B08_004.ab1 /Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results/test/K7_S6_280190.ab1
after unset t= v= n= a=U3244_AN_PN_11_03_11_2_B08_004.ab1 b=K7_S6_279891-280489
before assignment t= v= n= a=U3244_AN_PN_11_03_11_3_C08_006.ab1 b=K7_S7_279891-280489
before mv t=279891-280489 v=279891+280489 n=K7_S7_280190.ab1 a=U3244_AN_PN_11_03_11_3_C08_006.ab1 b=K7_S7_279891-280489
mv /Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results/test/U3244_AN_PN_11_03_11_3_C08_006.ab1 /Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results/test/K7_S7_280190.ab1
after unset t= v= n= a=U3244_AN_PN_11_03_11_3_C08_006.ab1 b=K7_S7_279891-280489
before assignment t= v= n= a=U3244_AN_PN_11_03_11_4_D08_008.ab1 b=K7_S8a_279891-280489
before mv t=279891-280489 v=279891+280489 n=K7_S8a_280190.ab1 a=U3244_AN_PN_11_03_11_4_D08_008.ab1 b=K7_S8a_279891-280489
mv /Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results/test/U3244_AN_PN_11_03_11_4_D08_008.ab1 /Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results/test/K7_S8a_280190.ab1
after unset t= v= n= a=U3244_AN_PN_11_03_11_4_D08_008.ab1 b=K7_S8a_279891-280489
before assignment t= v= n= a=U3244_AN_PN_11_03_11_5_E08_010.ab1 b=K7_S5_1083565-1084163
before mv t=1083565-1084163 v=1083565+1084163 n=K7_S5_1083864.ab1 a=U3244_AN_PN_11_03_11_5_E08_010.ab1 b=K7_S5_1083565-1084163
mv /Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results/test/U3244_AN_PN_11_03_11_5_E08_010.ab1 /Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results/test/K7_S5_1083864.ab1
after unset t= v= n= a=U3244_AN_PN_11_03_11_5_E08_010.ab1 b=K7_S5_1083565-1084163
before assignment t= v= n= a=U3244_AN_PN_11_03_11_6_F08_012.ab1 b=K7_S6_1083565-1084163
before mv t=1083565-1084163 v=1083565+1084163 n=K7_S6_1083864.ab1 a=U3244_AN_PN_11_03_11_6_F08_012.ab1 b=K7_S6_1083565-1084163
mv /Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results/test/U3244_AN_PN_11_03_11_6_F08_012.ab1 /Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results/test/K7_S6_1083864.ab1
after unset t= v= n= a=U3244_AN_PN_11_03_11_6_F08_012.ab1 b=K7_S6_1083565-1084163
before assignment t= v= n= a=U3244_AN_PN_11_03_11_7_G08_014.ab1 b=K7_S7_1083565-1084163
before mv t=1083565-1084163 v=1083565+1084163 n=K7_S7_1083864.ab1 a=U3244_AN_PN_11_03_11_7_G08_014.ab1 b=K7_S7_1083565-1084163
mv /Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results/test/U3244_AN_PN_11_03_11_7_G08_014.ab1 /Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results/test/K7_S7_1083864.ab1
after unset t= v= n= a=U3244_AN_PN_11_03_11_7_G08_014.ab1 b=K7_S7_1083565-1084163
before assignment t= v= n= a=U3244_AN_PN_11_03_11_8_H08_016.ab1 b=K7_S8a_1083565-1084163
before mv t=1083565-1084163 v=1083565+1084163 n=K7_S8a_1083864.ab1 a=U3244_AN_PN_11_03_11_8_H08_016.ab1 b=K7_S8a_1083565-1084163
mv /Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results/test/U3244_AN_PN_11_03_11_8_H08_016.ab1 /Users/pawannoel/Documents/Sequences/Sanger_Sequencing_results/test/K7_S8a_1083864.ab1
after unset t= v= n= a=U3244_AN_PN_11_03_11_8_H08_016.ab1 b=K7_S8a_1083565-1084163