Chemist Needs Help

Hello All I am a chemist and really dont know too much about programming but here goes.

Essentially I need to create a script that I can run which will do the following.
Create a file where the first four lines are a set of commands that are the same for all the files. E.g
ln1: comment
ln2: x y x
ln3: 101
ln4: 4 7

So the script should print those line into a new file first.

Then i need to make 4 columns.
The first I need to be 1.5 +0.1 100 times.
e.g.
1.5
1.6
1.7
1.8
1.9
etc.

The second column I need to be a column of data which I have used the command awk '/Done/ {print $2}' file
There will be 101 numbers extracted
Note: I would like Done and the number to be a variable to input in the command line.

The third and fourth are the same as the second except there will only be one point that I need to repeat 101 times.

If someone could help me that would be amazing. Also if someone is an expert I will pay them via paypal to help me occasionally. Thank you Lenny.

Well, you're short on exact information, so my attempt is generalized. It also isn't all that efficient. I picked 42 and 69.42 as the two fixed numbers.

printf "%s\n%s\n%s\n%s\n" "comment" "x y x"  "101"  "4 7" > newfile

awk '/Done/ {print $2}'  file | \
 awk ' BEGIN { cnt =1.5 }
         {printf("%.1f  %s  42 69.42\n", cnt  $0); cnt+=.1 } >> newfile

Where can I specify with better information

Jim, the first part is perfect. I will try to explain the columns a little better.

I need 4 columns:

The first should be 1.5 through 11.5 by 0.1 increments.

The second column will have 101 15-digit numbers which I have previously extracted with the awk command. The first 15-digit number should be in the same row as 1.5

The third column will have one 15 digit number that I will use the same awk function to retrieve with as i did before. But since I only have one number i need to repeat it 101. The same goes for the fourth.

In the end i should have 4 columns:

1.5 (15-digit number) (15-digit number) (15-digit number)
1.6 (15-digit number) (15-digit number) (15-digit number)

note thr last two columns will have the same 15 digit numbers going down the columns but each column will not have the same number.

Thanks, hope that helps.

The code above will work for you - plugin correct values for the red text below:

printf "%s\n%s\n%s\n%s\n" "comment" "x y x"  "101"  "4 7" > newfile

awk '/Done/ {print $2}'  file | \
 awk ' BEGIN { cnt =1.5 }
         {printf("%.1f  %s  42 69.42\n", cnt  $0); cnt+=.1 } >> newfile

Keep all quotes and punctuation characters as they are. They have meaning. Just use alphanumerics and . for a decimals and +- for signs on numbers if needed. File names cannot contain spaces or weird characters.

The number of rows you create in the output file is totally depdendent on the number of rows you read in via the first awk statement. ie., the number of lines read in from "file". Nothing is doing any counting for you. If you don't get 101 lines look to your input data.

Okay everything seems good but the values 42 and 69.42 also need to be extraced using the same awk command how do I put that command in there. Lenny

Are the values for the now-dummy 42 and 69.42 column also in the same file we are using to drive input?

No the dummy numbers will be from individuals files. For example my awk command will be run on three different files. Each file will correlate to each column. thank you Lenny

The numbers form the other files are just one single value or are they a column in the file?

Sorry I suck at explaining:

The second column I use the awk command to filter through a large file i have and pull out 101 numbers that are two delimitters after the word "Done" .

The third and fourth I use the same awk command but it does only pull out one value per file. That value I need to repeat 101 times. For column 3 and 4. Column three will get one value from "file2" and column 4 will get one value from "file3". Hope that helps. Lenny

Try this - you still need to edit filenames

awk '/Done/ {print $2; exit}'  file3 | read col3
awk '/Done/ {print $2; exit}'  file4 | read col4

printf "%s\n%s\n%s\n%s\n" "comment" "x y x"  "101"  "4 7" > newfile

awk '/Done/ {print $2}'  file | \
 awk -v col4="$col4" -v col3="$col3" ' BEGIN { cnt =1.5 }
         {printf("%.1f  %s  %s %s\n", cnt,  $0, col3, col4); cnt+=.1 } >> newfile

You can always make a big donation to the forums if you are pleased with your results here :smiley:

The UNIX and Linux Forums - PayPal Donate

I get this message:

../bin/Lenny: line 7: unexpected EOF while looking for matching `''
../bin/Lenny: line 9: syntax error: unexpected end of file

cnt+=.1 } '  >> newfile    # <---  last line of code 

Typo. My bad.

What about this error:

awk: cmd. line:1: (FILENAME=- FNR=1) fatal: not enough arguments to satisfy format string
`%.1f %s %s %s
'
^ ran out for this one

Thanks

I have a new error that reads as follows, I was wondering if anyone new how to solve this problem. Thanks for all the help Jim. Thanks, Lenny.

copy and paste precisely what you have for code. Not what I gave you, but what you are trying that throws the error. I do not get that error.

printf "%s\n%s\n%s\n%s\n" "comment" "x y x" "101" "4 7" > newfile
awk '/Done/ {print $2; exit}' Ar_b3lyp_pv5z.out | read col3
awk '/Done/ {print $2; exit}' Cl_b3lyp_pv5z.out | read col4
awk '/Done/ {print $2}' ArCl_b3lyp_pv5z.out | \
awk -v col4="$col4" -v col3="$col3" ' BEGIN { cnt =1.5 }
{printf("%.1f %s %s %s\n", cnt $0, col3 col4); cnt+=.1 }'>> newfile

Thats what I have the name of the files have been substituted. Thanks again.

printf "%s\n%s\n%s\n%s\n" "comment" "x y x" "101" "4 7" > newfile
awk '/Done/ {print $2; exit}' Ar_b3lyp_pv5z.out | read col3
awk '/Done/ {print $2; exit}' Cl_b3lyp_pv5z.out | read col4
awk '/Done/ {print $2}' ArCl_b3lyp_pv5z.out | \
awk -v col4="$col4" -v col3="$col3" ' BEGIN { cnt =1.5 }
    {printf("%.1f %s %s %s\n", cnt, $0, col3 col4); cnt+=.1 }'>> newfile

There was a missing comma after the word "cnt" on the last line. I ran it with dummy files, zero errors. Copy and paste this back into your script. There are no syntax errors.
Logic errors, maybe.

Still getting the error, could I attach the .out files

awk: cmd. line:1: (FILENAME=- FNR=1) fatal: not enough arguments to satisfy format string
`%.1f %s %s %s
'
^ ran out for this one

Thanks Jim.