Inserting a line when its length is variable

Hi Unix experts

I have simple text files in which the number of lines vary from one file to another. They look like the following:

#
#
.
.
 34    46 
 76    72
 39    68

I want to grab the first number of the last line of each file (let's say A= 39 in the above example), which is always a multiplication of 3, and insert a new line in the 28th line of another text file with the following format:

0, (A/3) +1, 0, 3,6,9,12,15,...,A

Where A can be a number between 3 and 54, so the number of fields in the line would be variable. Positions of zeros(0) in the line remain fixed. In case of A=39 the line lools like:

0, 14, 0,3,6,9,12,15,18,21,24,27,30,33,36,39

Any help is very much appreciated.

Regards

Your definition is not very clear and test data very limited , but try this:

awk '(NR==FNR) {A=$1} (NR!=FNR) & (FNR != 28) {FS=","; print;} (NR!=FNR) & (FNR==28) { $2 = A/3 + 1; $NF=A; print}'  file1 file2

where file1 is the first file with the last line of interest and file 2 is the file with the line you want to replace.

Let me elaborate more on this:

Let's call the file where I want to grab the number from file1 which may look like

#
#
.
.
 0  56
 3  34
 6  65
 9  98
 12 23
 15 78

So here the number I have to pull out is 15. Then I have to make changes to another file, named file2, i.e. insert a line on the 28th line with following format:

0, 6, 0, 3, 6, 9, 12, 15

Here A=15. Another example can be:

file1 
#
#
.
.
 0  12
 3  45
 6  78
 9  56
 12 34
 15 90
 18 23
 21 56
 24 8
 27 54
 30 23
 33 12

Then line #28 in file2 will be:

0,12, 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33

Prior to line insertion, file2 is a 50-line text file which will turn into a 51-line file after editing.

Please let me know if it is not yet clear.

Cheers

---------- Post updated at 11:10 AM ---------- Previous update was at 10:38 AM ----------

I just tried the code on Solaris but it gives syntax error:

Syntax error near line 1
bailing out near line 1

Thanks for your help