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 .
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.
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):
Thank you very much it helped me a lot Alister . 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.