Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Lots of file copyingand renaming? Post 80061 by hadarot on Wednesday 3rd of August 2005 01:04:10 AM
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..
 

8 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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
GENLIB_COPY_UP_SEG.3(October 1, 1997)									     GENLIB_COPY_UP_SEG.3(October 1, 1997)

NAME
GENLIB_COPY_UP_SEG - copy a physical segment from an instance in the current figure SYNOPSYS
#include <genlib.h> void GENLIB_COPY_UP_SEG(oldname, insname, newname) char *oldname, *insname, *newname; ORIGIN
This software belongs to the ALLIANCE CAD SYSTEM developed by the ASIM team at LIP6 laboratory of Universite Pierre et Marie CURIE, in Paris, France. Web : http://asim.lip6.fr/recherche/alliance/ E-mail : alliance-users@asim.lip6.fr PARAMETERS
oldname Name of the segment in the instance insname Name of the instance in the which the segment is to be searched for newname Name to be given to the segment in the current figure DESCRIPTION
COPY_UP_SEG adds a segment in the current layout cell. All the segment's attributes are copied from the first found segment called oldname in the instance insname. The new segment is given the name newname. The main purpose of this function is to rename a segment, so it can be used later, when extracting a layout. ERRORS
"GENLIB_COPY_UP_SEG impossible : missing GENLIB_DEF_PHFIG" No figure has been yet specified by a call to DEF_PHFIG. So it isn't possible to place a segment inside it. you must call DEF_PHFIG before any other layout action. "illegal getphins : instance insname does not exist" The instance called insname does not currently belong to the figure. "GENLIB_COPY_UP_SEG impossible : segment segname does not exist" The segment segname does not exist in the model of the instance insname. EXAMPLE
#include <genlib.h> main() { /* Create a figure to work on */ GENLIB_DEF_PHFIG("cell"); GENLIB_PLACE("z2l_y", "r1", NOSYM, 23L, 54L); /* Put a segment */ GENLIB_COPY_UP_SEG("mux0_s", "r1", "out_s"); /* Save that on disk */ GENLIB_SAVE_PHFIG(); } SEE ALSO
genlib(1), GENLIB_DEF_PHFIG(3), GENLIB_SAVE_PHFIG(3), GENLIB_PHSEG(3), GENLIB_THRU_H(3), GENLIB_THRU_CON_H(3), GENLIB_THRU_V(3), GEN- LIB_THRU_CON_V(3), GENLIB_WIRE1(3), GENLIB_WIRE2(3), GENLIB_WIRE3(3). BUG REPORT
This tool is under development at the ASIM department of the LIP6 laboratory. We need your feedback to improve documentation and tools. PROCEDURAL GENERATION LANGUAGE
ASIM/LIP6 GENLIB_COPY_UP_SEG.3(October 1, 1997)
All times are GMT -4. The time now is 10:49 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy