Adding (as in arithmetic) to numbers in columns in file, and writing new file with new numbers

Hi again. Sorry for all the questions � I've tried to do all this myself but I'm just not good enough yet, and the help I've received so far from bartus11 has been absolutely invaluable. Hopefully this will be the last bit of file manipulation I need to do.

I have a file which is formatted as follows:

ATOM      1  OW  SOL X 257      77.820  57.470  97.740  1.00  0.00
ATOM      2  HW1 SOL X 257      77.890  56.840  98.520  1.00  0.00
ATOM      3  HW2 SOL X 257      78.720  57.870  97.550  1.00  0.00
ATOM      4  OW  SOL X 258       2.150  71.800  96.120  1.00  0.00
ATOM      5  HW1 SOL X 258       2.940  72.410  96.120  1.00  0.00
ATOM      6  HW2 SOL X 258       2.370  70.970  96.630  1.00  0.00
ATOM      7  OW  SOL X 259      46.670  23.720  74.950  1.00  0.00
ATOM      8  HW1 SOL X 259      45.750  24.020  75.180  1.00  0.00
ATOM      9  HW2 SOL X 259      47.040  24.320  74.240  1.00  0.00
ATOM     10  OW  SOL X 260      61.950  15.110  16.890  1.00  0.00
ATOM     11  HW1 SOL X 260      61.360  15.690  17.450  1.00  0.00
ATOM     12  HW2 SOL X 260      62.900  15.430  16.960  1.00  0.00
ATOM     13  OW  SOL X 261       5.110  14.690  72.070  1.00  0.00
ATOM     14  HW1 SOL X 261       5.290  14.250  72.960  1.00  0.00
ATOM     15  HW2 SOL X 261       4.170  14.500  71.800  1.00  0.00

The coordinates there correspond to water molecules. In column 2 (beginning with 1 and ending with 15) I need to add 5294 to all the numbers and write a new file � or modify the original, but I assume this isn't possible � to give 5295 through 5309. In the third column, which has sets of three of the same five numbers (257 � 261) I need to do something similar, and subtract 126 from all of them (giving 131 � 135).

I should mention that the file itself has many thousands more of these lines, so ideally I need something which is acting on all the numbers in the respective columns. I figured awk but what I've tried isn't capable of handling the numbers as numbers, which is where I'm falling down, so I don't know if something like Python would work, but I've not had much luck there either and don't have much experience with the language.

The reason I need to do this is so that the software I'm using will be able to correctly read the coordinates I'm trying to combine.

Once again any help would be greatly appreciated. Many thanks.

Can we have sample output please

If you just need what you said, This is it.

$ awk '{$2+=5294;$6-=126;print}' OFS="\t" file
ATOM    5295    OW      SOL     X       131     77.820  57.470  97.740  1.00    0.00
ATOM    5296    HW1     SOL     X       131     77.890  56.840  98.520  1.00    0.00
ATOM    5297    HW2     SOL     X       131     78.720  57.870  97.550  1.00    0.00
ATOM    5298    OW      SOL     X       132     2.150   71.800  96.120  1.00    0.00
ATOM    5299    HW1     SOL     X       132     2.940   72.410  96.120  1.00    0.00
ATOM    5300    HW2     SOL     X       132     2.370   70.970  96.630  1.00    0.00
ATOM    5301    OW      SOL     X       133     46.670  23.720  74.950  1.00    0.00
ATOM    5302    HW1     SOL     X       133     45.750  24.020  75.180  1.00    0.00
ATOM    5303    HW2     SOL     X       133     47.040  24.320  74.240  1.00    0.00
ATOM    5304    OW      SOL     X       134     61.950  15.110  16.890  1.00    0.00
ATOM    5305    HW1     SOL     X       134     61.360  15.690  17.450  1.00    0.00
ATOM    5306    HW2     SOL     X       134     62.900  15.430  16.960  1.00    0.00
ATOM    5307    OW      SOL     X       135     5.110   14.690  72.070  1.00    0.00
ATOM    5308    HW1     SOL     X       135     5.290   14.250  72.960  1.00    0.00
ATOM    5309    HW2     SOL     X       135     4.170   14.500  71.800  1.00    0.00

If its not, Please elaborate as other posters said with expected output.
Btw, from column3, you mean column 6 right?

1 Like

Hmmm, maintaining the column separations is going to make it a little more difficult.
Are the column spacings critical?

Sorry for not being more specific.

The output that clx posted and the corresponding script were exactly what I needed � thank you very much.

The column spacings are indeed important but easy to modify manually in e.g. vi should the need arise; the reason I needed a script for this is because the actual data I need to use the script on contains thousands of lines of coordinates, so it would be totally unrealistic to do that part by hand.

Many thanks again.