How to make multiple copies?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to make multiple copies?
# 1  
Old 06-16-2012
How to make multiple copies?

At work I have to create multiple copies of a file all the time.
Example: I have a file called Sec30p01.txt
I need thirty of these, then I edit one line in each to make 30 different control files.
So I end up with Sec30p02.txt, Sec30p03.txt and so on up to 30
Currently I copy the first file thirty times, then I go in each file one by one using vi and edit one line in each.
Is there a way I can make the thirty files in one command using some kind of script?

Thanks

DaveSmilie
# 2  
Old 06-16-2012
Hi Dave,

What does the line you edit look like before and after the edit, it would be just as easy to do the edit inside the shell script as well.

Regards

Dave
# 3  
Old 06-16-2012
Hi.

Here is one way to create the copies:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate creation of a number of files.

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C

FILE=${1-data1}
pl " Input data file $FILE:"
head $FILE

rm p* ; touch p0 
pl " Status before file creation:"
ls p*

pl " Make n copies:"
LIST=$( printf "p%02d.txt " {7..14})
tee $LIST < $FILE > /dev/null
pl " Status after   file creation:"
ls p*

SAMPLE=p08.txt
pl " Sample of created file, $SAMPLE:"
head $SAMPLE

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39

-----
 Input data file data1:
Now is the time
for all good men
to come to the aid
of their country.

-----
 Status before file creation:
p0

-----
 Make n copies:

-----
 Status after   file creation:
p0  p07.txt  p08.txt  p09.txt  p10.txt	p11.txt  p12.txt  p13.txt  p14.txt

-----
 Sample of created file, p08.txt:
Now is the time
for all good men
to come to the aid
of their country.

See man pages for details.

Best wishes ... cheers, drl
# 4  
Old 06-16-2012
Quote:
Originally Posted by faaslave
Is there a way I can make the thirty files in one command using some kind of script?
Code:
file=Sec30p01.txt
n=2

while [ $n -le 30 ]
do
  case $n in
    ?) newfile=Sec30p0$n.txt ;;
    *) newfile=Sec30p$n.txt ;;
  esac
  cp "$file" "$newfile"
  n=$(( $n + 1 ))
done


Last edited by cfajohnson; 06-16-2012 at 08:11 PM..
# 5  
Old 06-16-2012
Dave, it it just a prompt. I am in a directory when i am doing this.

Quote:
file=Sec30p01.txt
fmt=Sec30p%02d.txt
n=2

while [ $n -le 30 ]
do
case $n in
?) newfile=Sec30p0$n.txt ;;
*) newfile=Sec30p$n.txt ;;
esac
cp "$file" "$newfile"
n=$(( $n + 1 ))
done
This looks like it could work. Would I have to type this every time or can this be saved as a script somehow? I am unsure how this works in unix, I have only made scripts in database design using filemaker.
I assume that $n is a variable and this will leave me with the copies I am looking for?

Thanks for all the replies

Dave
# 6  
Old 06-16-2012
Quote:
Originally Posted by faaslave
This looks like it could work. Would I have to type this every time or can this be saved as a script somehow? I am unsure how this works in unix, I have only made scripts in database design using filemaker.

Save the script in a file and make it executable:
Code:
script=my_script ## use a better name than my_script

echo 'file=Sec30p01.txt
n=2
while [ $n -le 30 ]
do
  case $n in
    ?) newfile=Sec30p0$n.txt ;;
    *) newfile=Sec30p$n.txt ;;
  esac
  cp "$file" "$newfile"
  n=$(( $n + 1 ))
done' > "$script"

chmod +x "$script"

Then you can execute it with
Code:
./my_script

Quote:
I assume that $n is a variable and this will leave me with the copies I am looking for?

$n is a variable that contains the number of the next copy. cp copies the file.
# 7  
Old 06-16-2012
Thanks,
So I want to make sure I understand everything

Quote:
script=my_script
Means my_script will execute the script

Quote:
echo 'file=Sec30p01.txt
This part might be a problem. Every time I would use this script the filename will be different. It could be Sec23p01.txt that I need copies of in sequential order or Sec31p01.txt.

And should this script have an extension of .txt or no extension?

Sorry, I am a total novice at this, but since the government won't pay for a programmer, people like me have to wing this stuff. I guess it is their way of saving the taxpayers money. lol

Dave
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to make faster loop in multiple directories?

Hello, I am under Ubuntu 18.04 Bionic. I have one shell script run.sh (which is out of my topic) to run files under multiple directories and one file to control all processes running under those directories (control.sh). I set a cronjob task to check each of them with two minutes of intervals.... (3 Replies)
Discussion started by: baris35
3 Replies

2. Shell Programming and Scripting

Print multiple copies page by page using lp command

Hi I have a pdf file that is being generated using the rwrun command in the shell script. I then have the lp command in the shell script to print the same pdf file. Suppose there are 4 pages in the pdf file , I need to print 2 copies of the first page, 2 copies of the second page , then 2... (7 Replies)
Discussion started by: megha2525
7 Replies

3. Shell Programming and Scripting

How to make this run in multiple threads

Hi, I have a list of URLs in a csv file which I'm checking for page status. It just prints the URL and the status as output. This works perfectly fine. I'm looking to run this in multiple threads to make this process faster. I'm pretty new to Perl and I managed to complete this. It would be... (9 Replies)
Discussion started by: kzenthil
9 Replies

4. Shell Programming and Scripting

Help configuring sudo for multiple file copies

Hiya, I want to allow some users to copy all filenames of a specific filetype, to a limited directory. 3+ users: need to be able to copy(as root) any *.war file to /usr/local/tomcat/current/webapps/ I tried the following... dmurphy huskar=/tmp/who.sh,/bin/cp *.war... (2 Replies)
Discussion started by: infinitiguy
2 Replies

5. UNIX and Linux Applications

How can I make multiple mirror websites on the fly?

-How can I make multiple mirror websites on the fly? How can I make multiple mirror websites on the fly, transparently, seemingly created, from multiple donor sites? For example hxxp://website.that-i-want-to-mirror.net/ to my hxxp://mymirror.website.com on dedicated server with WINDOWS or... (0 Replies)
Discussion started by: eurusd
0 Replies

6. UNIX for Dummies Questions & Answers

multiple copies at once

Dear all, Today I spend almost all my day with something I hope any of you can help me with... I'm trying to write a small script (!/bin/sh) to be able to make 900 copies of one file. Can anyone help me with this? I couldn't figure this out (maybe I need to create some sort of loop) and couldn't... (8 Replies)
Discussion started by: marjocello
8 Replies

7. UNIX for Dummies Questions & Answers

Printing Multiple Copies

Has anyone come accross and solved an issue where only 1 copy of a doc prints after you use the -n flag and specify more then 1. My exact syntax is lp -dprintername -n3 documentname enq command does same thing. Both commands show the 3 copies in the queue but only 1 ever prints I am using AIX... (0 Replies)
Discussion started by: capeme
0 Replies

8. Shell Programming and Scripting

Script to print multiple copies of the same file

Hi, this is my first post so I hope I have placed it in the appropriate section! I have created a looping script to print a text file multiple times. The script works great, and it displays the job numbers of all the prints that result from its execution. The trouble is, only the first job... (1 Reply)
Discussion started by: Alpha7
1 Replies

9. AIX

Printing multiple copies via remote queue

Hi all, I have an interesting problem. Using remote queues to print using jetdirect printers I am unable to get a printer to print multiple copies of a print job. command being used: lpr -P -#3 ./test (one copy prints) Also tried: enq -P -C3 ./test (same results) Any ideas? ... (6 Replies)
Discussion started by: Luck
6 Replies

10. UNIX for Advanced & Expert Users

lp not printing multiple copies

I'm trying to print multiple copies of a file on sun solaris 8 with the lp -n command to no avail. No matter what numeric value I supply -n, I still only get 1 copy of the file printed. The command I'm using is 'lp -d SNY_IT5000-2 -n2 file' The printer is an HP Laserjet 4000 with a... (2 Replies)
Discussion started by: hassan2
2 Replies
Login or Register to Ask a Question