Problem renaming files using variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem renaming files using variables
# 1  
Old 03-18-2013
Problem renaming files using variables

Hi, I have the following problem:

I have a list of files:
Code:
1.txt
2.txt
3.txt
4.txt

Then I have a list of variable names inside variable.txt:
Code:
A
B
C
D

I'd like to rename 1.txt, 2.txt etc using the variables from variable.txt
Code:
 
A.txt (Contains info from 1.txt)
B.txt (Contains info from 2.txt)
C.txt (Contains info from 3.txt)
D.txt (Contains info from 4.txt)

I've tried using this, but it doesn't work
Code:
END=4
for ((i=1;i<=END;i++)); do for j in $(cat variable.txt); do mv "$i".txt "$j".txt; done; done

When doing this, I get a list of files
Code:
 
A.txt  > (Contains info from 4.txt)
B.txt  > (Contains info from 4.txt)
C.txt  > (Contains info from 4.txt)
D.txt  > (Contains info from 4.txt)

but want

A.txt (Contains info from 1.txt)
B.txt (Contains info from 2.txt)
C.txt (Contains info from 3.txt)
D.txt (Contains info from 4.txt)

Something is going wrong with the loop and can't work out why it's doing this. I'm trying to learn scripting/loops and any help would be appreciated. Many thanks
# 2  
Old 03-18-2013
Hi, the error is the nested loops: try this way
Code:
END=4
for ((i=1;i<=END;i++))
do 
N=`sed -n ${i}p variable.txt`
mv ${i}.txt ${N}.txt
done

This User Gave Thanks to franzpizzo For This Post:
# 3  
Old 03-18-2013
That's fantastic franzpizzo, does exactly what I wanted. Thanks very much for your help Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem renaming files on Solaris 10 server

Good day all. I'm trying to rename some files in my home directory with some bizarre results. Basically I need to change the IP address in the filename to the hostname which I ggrep from within the file: -rw-r--r-- 1 bh694n nrc 5095 May 2 20:03 alarms_999.189.161.146.log... (2 Replies)
Discussion started by: BRH
2 Replies

2. Shell Programming and Scripting

Renaming multiple files in sftp server in a get files script

Hi, In sftp script to get files, I have to rename all the files which I am picking. Rename command does not work here. Is there any way to do this? I am using #!/bin/ksh For eg: sftp user@host <<EOF cd /path get *.txt rename *.txt *.txt.done ... (7 Replies)
Discussion started by: jhilmil
7 Replies

3. UNIX for Advanced & Expert Users

Problem with renaming files

I have about 1000 files containing the character * in the name. I need to find these files and replace the * with a -. I am working with HP UX v11. I am using the following command find . -type f -name '*\**' -exec bash -c 'f="$1"; mv "$f" "${f//\*/-}"' - '{}' \ People tell me it works for... (4 Replies)
Discussion started by: MikeDavid
4 Replies

4. Shell Programming and Scripting

Loop renaming files w/ a count problem

:wall: Hello there, basically in my program where im stuck at is when it comes to rename the files in a loop. - the program counts the number of files w a given name (works!) - and then if the number of files is greater or equal to the MAX_VERSIONS (numbers of files allowed w the... (1 Reply)
Discussion started by: thurft
1 Replies

5. Shell Programming and Scripting

File renaming problem

Can someone tell me how can i remove the RPCFTP word from RPCFTPfilelist.csv file ? (4 Replies)
Discussion started by: JSKOBS
4 Replies

6. Shell Programming and Scripting

Problem with renaming and saving

Hello mates, I am quite new to script programming and I am facing an uphill task to rename files in one folder. I have gone through similar posts but most of them deal with renaming files by changing the file extensions. Problem : I have a folder which contains files like... (5 Replies)
Discussion started by: chirag.joshi
5 Replies

7. Shell Programming and Scripting

renaming files or adding a name in the beginning of all files in a folder

Hi All I have a folder that contains hundreds of file with a names 3.msa 4.msa 21.msa 6.msa 345.msa 456.msa 98.msa ... ... ... I need rename each of this file by adding "core_" in the begiining of each file such as core_3.msa core_4.msa core_21.msa (4 Replies)
Discussion started by: Lucky Ali
4 Replies

8. UNIX for Advanced & Expert Users

Renaming Multiple Files that have variables in the name

I am trying to rename multiple files from lower case to upper case, but then I need to change the word SHIP to Ship and CSV to csv. This is a sample of the input file name input_file_ship_1234_20110106.csv I need this to look like this INPUT_FILE_Ship_1234_20110106.csv this is the result when I... (3 Replies)
Discussion started by: cventura
3 Replies

9. UNIX for Advanced & Expert Users

problem with renaming files

Hi, I need to rename all the .txt files present in current directory to .dat files respectively in UNIX. for example: $ ls aaa.txt bbb.txt ccc.txt I need to change them to $ ls aaa.dat bbb.dat ccc.dat Is there any UNIX command to do this in one go? ... (3 Replies)
Discussion started by: Johny001
3 Replies

10. UNIX for Advanced & Expert Users

advanced file renaming problem

I know this is probably a question for the newbie forum, where it is also posted, but I thought maybe some of you pros might like to help me out anyway. Here is my problem: I have to rename a batch of files that look like: 2001_0001.asc 2001_0002.asc . 2001_0548.asc 2002_0184.asc . .... (0 Replies)
Discussion started by: sea krait
0 Replies
Login or Register to Ask a Question