Making 256 files with variables

Hi,

I am really sorry for posting a newbie question here, but I do not have time to start learning shell scripting at the very moment.

My question is how can I write a script what takes a file with a variable number, changes the variable number to a new number and saves the file as a new file. I have 4 variables in one file and I need to change all of them four times.

That means all together I will have 4^4=256 new files and doing it manually just takes too much time.

Thank you in advance,

Mario

PS. If you have time could you explain what different rows do aswell, so I don't have to bother you guys again if I have something new to work with :smiley: .

Are you expecting like this.

See the following code, I am creating file name as variable value.

 var1=Welcome;
 eval >$var1

I didn't understand, what I need to change 4 variables for four times.

Hei, it's not exactly what I meant.

I will paste my file here, tho it is quite long. I added X, Y, Z, and M for the numbers I want to change.

#

Titaan-tetra-isopropksiid

0 1
Ti1
O2    Ti1    1.80
O3    Ti1    1.80    O2    109.00
O4    Ti1    1.80    O2    109.00    O3    120.00
O5    Ti1    1.80    O2    109.00    O4    120.00
C6    O2    1.40    Ti1    145.00    O3    0.00
H7    C6    1.10    O2    109.00    O2    X
C8    C6    1.40    O2    109.00    H7    120.00
H9    C8    1.10    C6    109.00    C6    0.00
H10    C8    1.10    C6    109.00    C6    120.00
H11    C8    1.10    C6    109.00    C6    240.00
C12    C6    1.40    O2    109.00    C8    120.00
H13    C12    1.10    C6    109.00    C6    0.00
H14    C12    1.10    C6    109.00    C6    120.00
H15    C12    1.10    C6    109.00    C6    240.00
C16    O3    1.40    Ti1    145.00    O4    0.00
H17    C16    1.10    O3    109.00    O3    Y
C18    C16    1.40    O3    109.00    H17    120.00
H19    C18    1.10    C16    109.00    C16    0.00
H10    C18    1.10    C16    109.00    C16    120.00
H21    C18    1.10    C16    109.00    C16    240.00
C22    C16    1.40    O2    109.00    C18    120.00
H23    C22    1.10    C16    109.00    C16    0.00
H24    C22    1.10    C16    109.00    C16    120.00
H25    C22    1.10    C16    109.00    C16    240.00
C26    O4    1.40    Ti1    145.00    O5    0.00
H27    C26    1.10    O4    109.00    O4    Z
C28    C26    1.40    O4    109.00    H27    120.00
H29    C28    1.10    C26    109.00    C26    0.00
H30    C28    1.10    C26    109.00    C26    120.00
H31    C28    1.10    C26    109.00    C26    240.00
C32    C26    1.40    O2    109.00    C28    120.00
H33    C32    1.10    C26    109.00    C26    0.00
H34    C32    1.10    C26    109.00    C26    120.00
H35    C32    1.10    C26    109.00    C26    240.00
C36    O5    1.40    Ti1    145.00    O2    0.00
H37    C36    1.10    O5    109.00    O5    M
C38    C36    1.40    O5    109.00    H37    120.00
H39    C38    1.10    C36    109.00    C36    0.00
H40    C38    1.10    C36    109.00    C36    120.00
H41    C38    1.10    C36    109.00    C36    240.00
C42    C36    1.40    O2    109.00    C38    120.00
H43    C42    1.10    C36    109.00    C36    0.00
H44    C42    1.10    C36    109.00    C36    120.00
H45    C42    1.10    C36    109.00    C36    240.00

Each letter (X,Y,Z,M) must be a number (60; 150; 240; 330). So I have 4 places where there could be 4 different numbers. All together I have 4^4 different files I want to have.

I hope it made the question easyer :slight_smile: .

Thank you in advance,

Mario

Not really sure your question, here is just a start.

The code finds out the lines which first column end of 7, and replace the last column.

awk '$1~/7$/ {$NF=($NF~"X")?"60":$NF;$NF=($NF~"Y")?"150":$NF;$NF=($NF~"Z")?"240":$NF;$NF=($NF~"M")?"330":$NF;}1' urfile

Hello, mario8eren:

If the file with the letters to be substituted is called "template", the following script will create 256 files in the current directory. The name of each file indicates the values that were substituted for each letter:

#!/bin/sh

template=$1
for values in {60,150,240,330}\ {60,150,240,330}\ {60,150,240,330}\ {60,150,240,330}; do
    set $values
    X=$1; Y=$2; Z=$3; M=$4
    sed "s/X\$/$X/;s/Y\$/$Y/;s/Z\$/$Z/;s/M\$/$M/" "$template" > "X$X-Y$Y-Z$Z-M$M"
done

Sample run (I did verify that proper substitutions were made in a few random files, but I did not inspect each one nor code something to verify):

$ ./mario8eren.sh template
$ ls
X150-Y150-Z150-M150     X240-Y150-Z150-M150     X330-Y150-Z150-M150     X60-Y150-Z150-M150
X150-Y150-Z150-M240     X240-Y150-Z150-M240     X330-Y150-Z150-M240     X60-Y150-Z150-M240
X150-Y150-Z150-M330     X240-Y150-Z150-M330     X330-Y150-Z150-M330     X60-Y150-Z150-M330
... listing truncated ...

Regards,
Alister

Thank you very much it helped me a lot Alister :slight_smile: . Also thanks to others for giving good ideas.

Mario

---------- Post updated at 07:16 AM ---------- Previous update was at 02:02 AM ----------

Hey guys, I ran into another problem today, if I add some lines to the script it works, but if I add another part it gives me an line syntax error. I coloured the non-working part red:

#!/bin/sh

template=$1
for values in {60,150,240,330}\ {60,150,240,330}\ {60,150,240,330}\ {60,150,240,330}; do
    set $values
    X=$1; Y=$2; Z=$3; M=$4
    mkdir "X$X-Y$Y-Z$Z-M$M"
    cp script ./"X$X-Y$Y-Z$Z-M$M"
    sed "s/X\$/$X/;s/Y\$/$Y/;s/Z\$/$Z/;s/M\$/$M/" "$template" > ./X$X-Y$Y-Z$Z-M$M/"X$X-Y$Y-Z$Z-M$M"
    cd ./X$X-Y$Y-Z$Z-M$M
    babel -igzmat "X$X-Y$Y-Z$Z-M$M" -oxyz coord.xyz
    source $USELIB/tm510
    x2t coord.xyz > coord
    define <<EOF


    a coord
    ired
    *
    *
    eht



    scf
    iter
    300

    dft
    on

    ri
    on

    *
    EOF

    qsub script
    cd ..
done

Thank you again in advance,

Mario

---------- Post updated 03-18-10 at 04:41 AM ---------- Previous update was 03-17-10 at 07:16 AM ----------

Little update, I added the "define part" of the file into another sh script and used sh command in the first script, so everything is working now as I wanted it.

Thank you all again :slight_smile: .

Mario