LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Renaming variables in a list (http://www.linuxquestions.org/questions/linux-newbie-8/renaming-variables-in-a-list-868581/)

Angeline 03-14-2011 07:48 PM

Renaming variables in a list
 
Hi,

I am trying to rename a list of variables in my script using a second list of variables. I want the variables in the second list to replace the variables in the first list such that the first variable in List 1 is renamed after the first variable in List 2, the second variable in List 1 is renamed after the second variable in List 2, the third variable in List 1 is renamed after the third variable in List 2, and so on. For example:

List1= 'sub1 sub2 sub3'
List2= 'A B C'

sub1.txt --> A.txt
sub2.txt --> B.txt
sub3.txt --> C.txt

I know how to rename each file individually, but would like to run Do Loop which can rename all my output files at once. Is this possible and can anyone please help?

Thanks,

Angeline

grail 03-15-2011 02:54 AM

Simply change your variables to arrays and step through:
Code:

#!/bin/bash

list1=(sub1 sub2 sub3)
list2=(A B C)

for i in ${!list1[*]}
do
    echo mv ${list1[i]}.txt ${list2[i]}.txt
done

If you remove the echo the move will take place. Should any of the file names have spaces you will need to quote your variables.

archtoad6 03-15-2011 11:39 AM

grail,

Great use of echo as a safety:
Code:

echo mv ${list1[i]}.txt ${list2[i]}.txt
Did you know that this works also:
Code:

$e mv ${list1[i]}.txt ${list2[i]}.txt
where $e is toggled between 'echo' (e='echo') & '' (e='') -- great for using the technique on multiple lines.

Angeline 03-15-2011 03:06 PM

Thanks for the suggestion. I tried that out, but the files don't seem to be renamed. This is what I have exactly:

#!/bin/bash

pwd='/path/to/my/data/files'

list1='subject00000 subject00001'
list2='A B'

for i in ${!list1[*]}
do

cd ${pwd}
mv ${list1[i]}.txt ${list2[i]}.txt

done


I keep getting the following error:

mv [-f | -i | -n] [-v] source target
[-f | -i | -n] [-v] source ... directory

Do you have any advice? Please and thank you!

archtoad6 03-15-2011 04:00 PM

1st, please put your code in "Code" blocks.

Next, grail recommended:
Code:

list1=(sub1 sub2 sub3)
list2=(A B C)

not:
Quote:

Originally Posted by Angeline (Post 4291750)
list1='subject00000 subject00001'
list2='A B'

You have to use the parentheses to set up the array variables that his method is based on.

grail 03-15-2011 06:42 PM

Also, putting the 'cd' inside the loop is a waste as you only need to go to the directory once. Try placing it before the loop.
Plus if you had of used the echo that was recommended, you would have seen the issue as outlined by archtoad6.

Angeline 03-17-2011 12:27 PM

Hi,

I tried exactly what you just said and it seems to have worked perfectly. Amazing! Thank you so much. I appreciate the help.

Angeline

grail 03-17-2011 12:35 PM

No problem ... don't forget to mark as SOLVED once you have a solution.


All times are GMT -5. The time now is 08:25 PM.