LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 03-14-2011, 06:48 PM   #1
Angeline
LQ Newbie
 
Registered: Mar 2011
Posts: 3

Rep: Reputation: 0
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
 
Old 03-15-2011, 01:54 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,176

Rep: Reputation: 1779Reputation: 1779Reputation: 1779Reputation: 1779Reputation: 1779Reputation: 1779Reputation: 1779Reputation: 1779Reputation: 1779Reputation: 1779Reputation: 1779
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.
 
1 members found this post helpful.
Old 03-15-2011, 10:39 AM   #3
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 230Reputation: 230Reputation: 230
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.
 
Old 03-15-2011, 02:06 PM   #4
Angeline
LQ Newbie
 
Registered: Mar 2011
Posts: 3

Original Poster
Rep: Reputation: 0
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!
 
Old 03-15-2011, 03:00 PM   #5
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 230Reputation: 230Reputation: 230
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 View Post
list1='subject00000 subject00001'
list2='A B'
You have to use the parentheses to set up the array variables that his method is based on.

Last edited by archtoad6; 03-15-2011 at 03:04 PM.
 
Old 03-15-2011, 05:42 PM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,176

Rep: Reputation: 1779Reputation: 1779Reputation: 1779Reputation: 1779Reputation: 1779Reputation: 1779Reputation: 1779Reputation: 1779Reputation: 1779Reputation: 1779Reputation: 1779
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.
 
Old 03-17-2011, 11:27 AM   #7
Angeline
LQ Newbie
 
Registered: Mar 2011
Posts: 3

Original Poster
Rep: Reputation: 0
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
 
Old 03-17-2011, 11:35 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,176

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


Reply

Tags
bash, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variables in moving / renaming files using CURL to download Freaksta Linux - Software 3 08-30-2010 09:13 AM
How to list the variables that are used in a script? alexandrum77 Linux - Newbie 2 05-10-2010 03:23 AM
Get strings from list using variables mwade Programming 5 10-28-2006 08:01 PM
(bash) how to list all variables? Duudson Programming 11 03-24-2005 12:35 PM
How can I get the full list of environment variables (RedHat 7.2)? yuzuohong Linux - General 1 06-07-2003 03:28 AM


All times are GMT -5. The time now is 11:32 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration