Making 256 files with variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Making 256 files with variables
# 1  
Old 03-12-2010
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 Smilie .
# 2  
Old 03-12-2010
MySQL

Are you expecting like this.

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

Code:
 var1=Welcome;
 eval >$var1


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

Last edited by ungalnanban; 03-12-2010 at 04:57 AM..
# 3  
Old 03-16-2010
Quote:
Originally Posted by ungalnanban

Code:
 var1=Welcome;
 eval >$var1

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.

Code:
#

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

Thank you in advance,

Mario
# 4  
Old 03-16-2010
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.
Code:
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


Last edited by rdcwayx; 03-16-2010 at 08:03 PM..
# 5  
Old 03-16-2010
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:

Code:
#!/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):
Code:
$ ./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

Last edited by alister; 03-16-2010 at 10:33 PM..
# 6  
Old 03-18-2010
Quote:
Originally Posted by alister
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:

Code:
#!/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

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

Code:
#!/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 Smilie .

Mario
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need script for making files based on some conditions.

Hi All, I have a text file (code_data.txt) with the followig data. AMAR AB123456 XYZ KIRAN CB789 ABC RAJ CS78890 XYZ KAMESH A33535335 ABC KUMAR MD678894 MAT RITESH SR3535355... (26 Replies)
Discussion started by: ROCK_PLSQL
26 Replies

2. Shell Programming and Scripting

Making a script to copy files not seen before (using md5sum)

Hello, I would like to make a script that searches through a SRC folder and copies only files it's never seen before to a DEST folder. SRC = /user/.phonesync/photos-backup DST = /usr/.phonesync/photos-new So basically, I'd start with a: md5sum /user/.phonesync/photos-backup/* >... (29 Replies)
Discussion started by: nbsparks
29 Replies

3. UNIX for Advanced & Expert Users

Move files while making a tar

I have the following folder structure code/f1/ code/lib/t1 code/lib/t2 code/lib/t3 code/lib/t3 code/lib_1/t1 code/exc I would like to create a tar with a folder structure below and I can use the following tar command f1 lib/t1 lib/t2 lib/t3 tar -cvf code.tar -C code f1 lib... (4 Replies)
Discussion started by: alpboys
4 Replies

4. Shell Programming and Scripting

[PHP] Need help with making variables Global

I have made a script that requires another php script for functions. I need a way so that the required script can read and write the main script's variables. Best Regards, John Wei ---------- Post updated at 08:54 AM ---------- Previous update was at 08:40 AM ---------- Sorry Guys, EDIT: my... (1 Reply)
Discussion started by: johntzwei
1 Replies

5. Shell Programming and Scripting

Making Variables

Dear Friends, Here I need your help once again. I have a flat file with pipe de-limited format e.g. 12345|1234567890|0|0|0| (Total 5 values) I want to take all non 0 ("Zero") values in variables named as anu1, anu2, anu3, anu4 and anu5. Is it possible? Please guide me. Thank you in... (3 Replies)
Discussion started by: anushree.a
3 Replies

6. Shell Programming and Scripting

[problem] making a backup files

Hi guys, I'm a little stuck on this problem, I've tried googling it and some trial and error but can't seem to get it working. Basically I need the script to: 1) create a directory called "mybackups", if it doesn't exist 2) go through all the .sh files in the current directory, and copy... (4 Replies)
Discussion started by: chazij
4 Replies

7. Shell Programming and Scripting

Making file with values from different files

Hello, I am stuck up in middle of a script.Pls have a look at the problem and help me in any way out for the same. There are n number of files with n number of contents in a column. for example : file1 has contents in quotes "abcd" "1234-asbcd" "12312"..... file2 has contents in... (4 Replies)
Discussion started by: er_aparna
4 Replies

8. UNIX for Dummies Questions & Answers

Making files readonly with vi?

Hi, I'm new at this whole Unix thing, but definately learning (lots of fun)... and I was wondering - how do you make a file read-only with vi? I don't mean how do you load up vi in read-only mode, but how do you save a file (or flag a file, or whatever) in vi to read-only? How do you make it... (2 Replies)
Discussion started by: Flyguy
2 Replies

9. Filesystems, Disks and Memory

pls hlp: making .sh files executable

I've got a file named jdictd.sh containing the following: --- begin file contents --- #! /bin/csh echo I\'m running! java -cp jdictd.jar org.dict.server.JDictd data/dict.ini # Use the following line instead if JRE 1.1 is used # jre -cp jdictd.jar org.dict.server.JDictd data/dict.ini ---end... (2 Replies)
Discussion started by: ropers
2 Replies
Login or Register to Ask a Question