Lots of file copyingand renaming?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Lots of file copyingand renaming?
# 8  
Old 07-29-2005
Quote:
Originally Posted by AeroEngy
It will be something like this YYMMDD_LongDesciption_of_File.mat changed to Totally_different_Char_string_MMDDYY.mat about a thousand times.
Quote:
Originally Posted by fdarkangel
it'd help if you tell what conversion you need exactly, though.
ugh. what's the relation between LongDesciption_of_File and Totally_different_Char_string (is there a rule to produce latter one from the first one, or are they just constants)? Are MM, DD and YY fields guaranteed to be 2 characters each?
# 9  
Old 07-30-2005
Quote:
ugh. what's the relation between LongDesciption_of_File and Totally_different_Char_string (is there a rule to produce latter one from the first one, or are they just constants)? Are MM, DD and YY fields guaranteed to be 2 characters each?
There is very little relationship between the new and old naming format. They are files reduced from hundreds of tests and it seems like every time they performed the same test they called it something different (no naming convention). There is no rule to produce the new name from the old. You just have try to interpret the old name and figure out what they are talking about and rename it accordingly to a standard convention. That way we can catalog all these data files and be able to actually find something when we need to do some research. Luckily someone has come up with a table(text file) of old file name to new ones for every single file that we have. That is why I would like to be able to read from the text file into a script to do all of the copying and renaming.

Quote:
Try something like:

Code:
#! /usr/bin/ksh

exec < /file/with names
while read oldname newname ; do
       echo cp /old/dir/$oldname /new/dir/$newname
done
exit 0

I will give it a shot on monady when I get back to work. I don't have a Unix machine at home to try anything.

Thanks,
# 10  
Old 08-01-2005
Here is what I got to finally work (almost).

Code:
while read -r $oldname $newname; do
cp /path/$oldname /path/$newname
done < /path/to/textfile.txt

The only problem I have with the code is how it reads the text file. It sees the line feed at the end of each line of the text file and adds a character onto the end of the file extension that looks like a little square. So instead of whatever.mat I get whatever.mat"littlesquare". This symbol can only be seen if you look at the files using a windows machine. Everything looks normal when viewing the files in Solaris. However, I can edit the text file so there is a cariage return at the end of each line instead of a line feed. It is just a pain in the neck. If anyone knows a way to fix this without changing the text file I would appreciate it.

Thanks
# 11  
Old 08-01-2005
Probably copied from a microsoft system incorrectly. Look in our faq section for "text files, ASCII files, binary files and ftp transfers"
# 12  
Old 08-03-2005
This discussion will be seem clearer afer examining the final code offered--which is very short. But to gain some knowledge and understanding, which is always importnat...
The read command works by using the characters stored in the IFS variable -- "internal field separator", by default whitespace -- to separate out the tokens/fields on each line it reads, and then loads the variables specified at its arguments, one token per variable. But if any tokens remain, they are all added to the last variable specified. So when you say read oldname newname, 2 variables are specified. The first parsed token is loaded into oldname, and the remainder of the tokens into newname. I.e., the first filename is being loaded into the var oldname, but newname is getting extra stuff besides the 2nd filename (the carriage return, or anything else after the second filename). (MS uses '\r\n' --CR/LF--to end lines, Unix just '\n' (i.e., LF)). So then one might think the following would work, which has a "garbage collecting" third variable for read:
Code:
#!/bin/sh
while read -r oldname newname trash; do
cp /path/$oldname /path/$newname
done < /path/to/textfile.txt

The previous code works only if there is some whitespace after the destination (2nd) filename on each line of the file. If so, the '\r' and the filename will be separated by whitesapce, and since $IFS is whitespace, read will treat the '\r' as a separate, third token, and it will be loaded into our trash variable.
If there is no space after the 2nd filename, the second token will be "dest_file\r", in which case you first have to get rid of the carriage returns (if you don't, the 2nd argument to your cp command will be meesed up). The filter tr -d '\015' will remove a carriage return. (015 is the octal ASCII code for carriage return, and tr -d deletes the specified character).
Even so, you may still may want to use a third "garbage collecting" variable, in case you have any comments in the file, which would mess up your cp command. E.g.
Code:
old_checkbook.dat  new_checkbook.dat  ' my checkbook data

Just make sure there are no spaces in the filenames you have. Otherwise, hopefully you have used a non-whitespace to delimit the source filename form the target filename. (If so, script will need to be fancier.)
Also, if the filenames in the file are from an MS machine, and you want to put the files on a Unix box, make sure the directory delimiter is '/', not MS '\'. You can do that with the filter tr '\' '/'
So in the end, we end up with this, whi h is basically just a more robust version of Perderabo's code:
Code:
#!/bin/sh
tr -d '\015' < /path/to/textfile.txt | tr '\' '/' | while read -r oldname newname trash
do
   cp "/path/$oldname" "/path/$newname"
done


Last edited by hadarot; 08-08-2005 at 10:22 PM..
# 13  
Old 08-03-2005
wouldn't something like this work?

Code:
list=$(awk '{printf $1" "}' listfile)
for oldname in $list; do
        newname=$(grep $oldname listfile|awk '{print $2}')
        mv $oldname $newname
done

 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove answers and explanation parts from a text file with lots of questions.?

Hi, I have a text file with thousands of questions in it. Each question (multiple lines) with multiple choice options, Answer and Explanation (optional). I need to delete Answer & explanation parts for all Questions and insert a blank line before net question. Each question starts with NO. I... (4 Replies)
Discussion started by: prvnrk
4 Replies

2. Shell Programming and Scripting

How to rename lots of files with find?

Can someone help me with this script. I have a bunch of files like this: "2209OS_02_Code" "2209OS_03_Code" "2209OS_04_Code" "2209OS_05_Code" "2209OS_06_Code" "2209OS_07_Code" "2209OS_08_Code" "2209OS_09_Code" "2209OS_10_Code" "2209OS_10_video" and I want to rename them to be like this: ... (2 Replies)
Discussion started by: siegfried
2 Replies

3. Shell Programming and Scripting

Lots of sed

Hi, I have 200 lines of sed commands in a shellscript sed s/TSTARTO2GPRSEVENTAPNACCSUM_1/TSTARTO2GPRSEVENTAPNACCSUM_24/g sed s/O2GPRSEVENTAPNACCSUM_1/O2GPRSEVENTAPNACCSUM_24/g sed s/TENDO2GPRSEVENTAPNACCSUM_1/TENDO2GPRSEVENTAPNACCSUM_24/g sed s/BSTARTO2EVENTITEM_1/BSTARTO2EVENTITEM_24/g... (6 Replies)
Discussion started by: legolad
6 Replies

4. IP Networking

netstat showing lots of errors

Hi, I have a server which receive lots of events to it and when I do netstat -Ie1000g0, I'm seeing the errors as below. Name Mtu Net/Dest Address Ipkts Ierrs Opkts Oerrs Collis Queue e1000g0 1500 abc-1.com abc-1.com 3093708246 12757 92069412 0 0 0 If anyone could direct me to right... (3 Replies)
Discussion started by: mohzub
3 Replies

5. AIX

Lots of page faults and free memory

Hello, I've been reading your forums for quite a while and the great amount of information I find here always come in hand.This time however, I need some specific help... I have a doubt with an AIX server which I'm failing to understand as I'm new to its concept of memory management... ... (8 Replies)
Discussion started by: flpgdt
8 Replies

6. Programming

problem with lots of open sockets

I'm programming in C with lots of sockets, (asynchronous handling), and for a while it goes ok, but after a while it starts to hang, or not get complete connections anymore. checking my router, i see that i get nat tables overflow and even my DNS cacher seems to be having serious issues... ... (4 Replies)
Discussion started by: alien999999999
4 Replies

7. Red Hat

Lots of questions about linux.

I am about 2 months new to Linux, and have only limited experience with PCLinuxOS, (kde) and Redhat. I am installing the latest version of PClinuxOS on my wife's PC right now but running into a problem I dont understand. I have never seen it before so i thought i'd ask how I can get around it.... (1 Reply)
Discussion started by: Methal
1 Replies

8. UNIX for Dummies Questions & Answers

Deleting lots of files.....

Hi All, Thanks in advance for reading and any posts... I have to delete a lot of files (about 6 pages of a4 (ls -ltr)) but I have to keep some as well. I would normally do an rm * to get rid of them all, but thats not what I want to do. Is there anyway I could rm * but add in a list of... (8 Replies)
Discussion started by: B14speedfreak
8 Replies
Login or Register to Ask a Question