help with a 'while read' loop to change the names of files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help with a 'while read' loop to change the names of files
# 1  
Old 03-01-2008
help with a 'while read' loop to change the names of files

Hi,

I am new to scripting, so any help on this would be much appreciated.

I am trying to rename a bunch of files, taking the names sequentially from a list read in another file...

# ls oldnames
file_1
file_2
file_3

# cat names
red
yellow
green


I want the files to take on the names red yellow green as they are read line by line from the file "names"
ie
cp oldnames/file_1 newnames/red
cp oldnames/file_2 newnames/yellow
cp oldnames/file_3 newnames/green
etc....

What I have tried (but doesn't work) -

#! /bin/bash
ls . |while read line
do
echo $line
cat names | while read xx
do
cp $line ../newnames/$xx
done

done

This creates new files but only copies the last entry from the ls to each of them Smilie

As you can see my scripting skills are not great - any ideas would be much appreciated.
# 2  
Old 03-01-2008
to start with:
invoked from the parent old 'oldnames' direcotry. Assumes 'oldnames' and 'newnames' direcotries are SIBLINGS and the file 'names' resides in the parent of 'oldnames'.
Adjust to fit the actual requirement.
Good luck!
Code:
#!/bin/ksh

ls oldnames | while read old
do
   while read new
   do
     cp oldnames/"${old}" newnames/"${new}"
   done < names
done

# 3  
Old 03-01-2008
Code:
#!/bin/sh

path="/path/oldnames"
newpath="/path/newname"
ls $path | awk -v path=$path -v newpath=$newpath ' pass the variables path and newpath to awk
BEGIN{ 
  # this "BEGIN" block will be executed first before any input record is processed
 q="\047"  # set variable q to contain the single quote
 while( (getline f < "names" ) > 0 ) { 
   # get the contents of "names" file, using f to store each line as the while loop iterates the file
   # then store the value of each line (f) to array "a". "c" is the index of the array
   a[++c]=f
 } 
 close("names") # close the file
}
{
  # process the results of "ls" command, putting each file listed into array "b", and increment the index
 b[++d]=$0  # 
}
END{
  # the below is done at the very end of processing the inputs.
  for ( i=1; i<=c;i++) {
    cmd="cp "q path"/"b[i] q" "q newpath"/"a[i]q
    print cmd
    #system(cmd) #uncomment to execute
  }
}
'


Last edited by ghostdog74; 03-01-2008 at 10:04 PM..
# 4  
Old 03-01-2008
thanks guys I will give these a try - your help is much appreciated
Smilie
# 5  
Old 03-01-2008
vgersh99 I tried tinkering with the script you gave me but still couldn't get it working - still struggle with basic scripting.

ghostdog74 - script worked a treat, I just wish I could understand what everything is doing

thanks again guys.
# 6  
Old 03-01-2008
starsky,
reread the 'assumptions' and adjust the implemntation to fit the 'reality'.
# 7  
Old 03-01-2008
I think You would be helped with an array, assuming the number of files in oldnames is equal to the number of names in the list names. In bash it would be something like:

Code:
mkdir newnames
cnt=0
newname=($(< names))
for x in oldnames/*; do
cp $x newnames/${newname[$cnt]}
cnt=$(($cnt+1))
done

/Lakris
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Read .xlsx and text files in a loop using openpyxl

I have a list of .xlsx files (names 1.xlsx, 2.xlsx etc) in a directory, on which I need to insert data from its corresponding text file (named 1.txt, 2.txt etc) on the second worksheet named 'Filtered' and save it. The code I am trying is #!/usr/bin/python import os from... (8 Replies)
Discussion started by: nans
8 Replies

2. Shell Programming and Scripting

While loop a file containing list of file names until the files are found?

Hi, I have a control file which will contain all filenames(300) files. Loop through all the file names in the control files and check the existence of this file in another directory(same server). I need to infinitely(2 hrs) run this while loop until all the files are found. Once a file is found,... (5 Replies)
Discussion started by: laknar
5 Replies

3. Shell Programming and Scripting

Read loop from two files using descriptors

What I would like to do is read each line in the atdinfile: A sample atdinfile would look like this: 651 652 653 654 655 656 657 658 659 660 661 664 665 666 667 668 (5 Replies)
Discussion started by: woodson2
5 Replies

4. Shell Programming and Scripting

For loop inside awk to read and print contents of files

Hello, I have a set of files Xfile0001 - Xfile0021, and the content of this files (one at a time) needs to be printed between some line (lines start with word "Generated") that I am extracting from another file called file7.txt and all the output goes into output.txt. First I tried creating a for... (5 Replies)
Discussion started by: jaldo0805
5 Replies

5. UNIX Desktop Questions & Answers

Change name of files to their paths -- find loop

Dear All, I have many sub-folders but each of them have a file with same name but different data. I want to either move or copy them into a new folder but they need to have the path of where they are coming as part of their name... I have managed to find the files but dont know how to change... (2 Replies)
Discussion started by: A-V
2 Replies

6. Shell Programming and Scripting

Merging two columns from two files with similar names into a loop

I have two files like this: fileA.net A B C fileA.dat 1 2 3 and I want the output output_expected A 1 B 2 C 3 I know that the easier way is to do a paste fileA.net fileA.dat, but the problem is that I have 10,000 couple of files (fileB.net with fileB.dat; fileC.net with... (3 Replies)
Discussion started by: valente
3 Replies

7. UNIX for Dummies Questions & Answers

[Solved] Writing a loop to changing the names of files in a directory

Hi, I would like to write a loop to change the names of files in a directory. The files are called data1.txt through data1000.txt. I'd like to change their names to a1.txt through a1000.txt. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

8. Shell Programming and Scripting

Read and edit multiple files using a while loop

Hi all, I would like to simply read a file which lists a number of pathnames and files, then search and replace key strings using a few vi commands: :1,$s/search_str/replace_str/g<return> but I am not sure how to automate the <return> of these vis commands when I am putting this in a... (8 Replies)
Discussion started by: cyberfrog
8 Replies

9. Shell Programming and Scripting

Change files names

HI, Is there any possibility to change names of many files using "mv" and "for" loop ??? For ex. I have many txt files. file1.txt file2.txt ... And I tried something like this, but it's not working for (( i=1 ; $i<=10 ; i++ )) ; do mv file$i.txt newfile$1.txt; done (3 Replies)
Discussion started by: Physix
3 Replies

10. Shell Programming and Scripting

help writing script to read files names

Hi there, I am trying to do somehting similar, but on a wider scale. I am trying to write a script that would open the home directory, open the first (of 650) user's folder open the ?mail directory, which every user has Then I need the script to read each of the files and folder names with... (2 Replies)
Discussion started by: technett
2 Replies
Login or Register to Ask a Question