Noob's 1st...bash-script for copying one file into many

I have one file "file.a.b.c-d.r" that I would like to use to spawn 4 other files:

"file.a.b.1-A.r"
"file.a.b.1-B.r"
"file.a.b.1-C.r"
"file.a.b.1-D.r"

where the field "c-d" changes into my 1 and A-D.

I was doing this manually at the prompt with

> cp "file.a.b.c-d.r" "file.a.b.1-A.r"
> cp "file.a.b.c-d.r" "file.a.b.1-B.r"
...etc..

but I think I should tackle a bash script

I figure I have to declare the file name to be used as a template

#! /bin/sh

#file to use as template

temp = "~/file.a.b.c-d.r"

then copy it 4 times, while modifying 1 and A-D fields, but I'm not sure where to even start....hence the noob part...

sorry to be so dense.
Any help is appreciated.

#! /bin/ksh

#file to use as template

temp = "~/file.a.b.c-d.r"
while read line
do
    cp "${temp}" ${line}
done < listOfFilesFile

The 'listOfFilesFile' is the file containing the list of files you'd like to 'spawn to'.

Thanks vgersh99!
so, this "listOfFilesFile" should be a directory I should have already created and where the new files will be dumped to?

No. The 'listOfFilesFile' is the name of the file containing the filenames you'd like to 'spawn' your 'temp' file to.
Am I missing something in your original request?

Ah...sorry I think I was a bit unclear.
My goal is to take a file with name "file.a.b.c-d.r" and produce 4 exact copies of it with 4 different names:
"file.a.b.1-A.r"
"file.a.b.1-B.r"
"file.a.b.1-C.r"
"file.a.b.1-D.r"

all the while, being able to "feed" the four new variables A, B, C, D I name within the bash script.

Thanks again for helping me out.

something along these lines...
remove the 'echo' when satisfied with the results.

#!/bin/ksh

temp='~/file.a.b.c-d.r'
vars='A B C D'

for var in ${vars}
do
    a="${temp%-*}"
    a="${a%.*}"
    a="${a}.1-${var}.${temp##*.}"
    echo cp "${temp}" "${a}"
done

Thank you sir.
I see what you're doing.

  1. you set the template
  2. you set the variables
  3. there's a "for-loop"
    a. I guess now I have to familiarize myself with variable assignment and regular expreession matching to figure out what is going on within the curly brackets and with the "%" and "-" characters.

After taking off the "echo" command and assigning the the right path, it does spit out the 4 files I wanted...
Thanks again for your help!

OK so I figured out what each part of that little script in the for loop (along with why you use curly brackets when calling a variable)

a="${temp%-*}" ##cuts everything after the c off of the name of temp b/c using remainder command

a="${a%.*}" ##cuts the ".c" part.....though I'm not sure why this was needed since you could've just cut it in the first assignment of a by cutting before the "-"

a="${a}.1-${var}.${temp##*.}" ##calls for a adds "a .1-" then each of the variables {A B C D} but the last part has be stumped

how does ${temp##*.}" work ?

thanks again

'man ksh' yields the following:

     ${parameter%word}
           Remove Smallest  Suffix  Pattern.  The  word  will  be
           expanded to produce a pattern. The parameter expansion
           then will result in parameter, with the smallest  por-
           tion of the suffix matched by the pattern deleted.

     ${parameter%%word}
           Remove  Largest  Suffix  Pattern.  The  word  will  be
           expanded to produce a pattern. The parameter expansion
           then will result in parameter, with the  largest  por-
           tion of the suffix matched by the pattern deleted.

     ${parameter#word}
           Remove Smallest  Prefix  Pattern.  The  word  will  be
           expanded to produce a pattern. The parameter expansion
           then will result in parameter, with the smallest  por-
           tion of the prefix matched by the pattern deleted.

     ${parameter##word}
           Remove  Largest  Prefix  Pattern.  The  word  will  be
           expanded to produce a pattern. The parameter expansion
           then will result in parameter, with the  largest  por-
           tion of the prefix matched by the pattern deleted.

Given the above description, try to work out the mechanics with a sample input.

1 Like

a little shorter:

temp=~/file.a.b.c-d.r
for var in {A..D}; do
   echo "cp $temp ${temp%?-*}1-$var${temp#*-?}"
done

Ah nice! I think I can probably benefit from the long code since it make me think about the nuts and bolts. This is a steep learning curve. Thanks.

You're definitely better off learning the long way, but here's a shorty...

tee file.a.b.{A,B,C,D}.r < file.a.b.c-d.r

Or, same thing but slightly longer and easier to understand:

cat file.a.b.c-d.r | tee file.a.b.{A,B,C,D}.r

The shell expands the things in curly brackets so

file.a.b.{A,B,C,D}.r

becomes the 4 files

file.a.b.A.r
file.a.b.B.r
file.a.b.C.r
file.a.b.D.r

and tee splits the output of the original file to all the files listed. The second case uses cat to display the file and pipe it to tee, the first case just uses the contents of the file as input for tee.

1 Like

Cool!...somehow pipe fitting is more intuitive to my biologist brain, than figuring out prefix and suffix patters...

yes, really cool
but the output filenames are hard coded.

{k,ba,}sh can do it "softly"