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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding (as in arithmetic) to numbers in columns in file, and writing new file with new numbers
# 1  
Old 04-05-2014
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:
Code:
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.

Last edited by crunchgargoyle; 04-05-2014 at 03:24 PM.. Reason: Minor clarity edit
# 2  
Old 04-05-2014
Can we have sample output please
# 3  
Old 04-05-2014
Quote:
Originally Posted by crunchgargoyle
In column 2 (beginning with 1 and ending with 15) I need to add 5294 to all the numbers and write a new file. 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).
If you just need what you said, This is it.

Code:
$ 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?
This User Gave Thanks to clx For This Post:
# 4  
Old 04-05-2014
Hmmm, maintaining the column separations is going to make it a little more difficult.
Are the column spacings critical?
# 5  
Old 04-05-2014
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.

Last edited by crunchgargoyle; 04-05-2014 at 05:28 PM.. Reason: Edit: also yes, sorry, I did mean column 6
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - Adding and Subtracting Numbers from 2 Columns

Hi Folks, I have a file with 2 columns TAB delimited and I want to add '1' to the first column and subtract '-1' from the second column. What I have tried so far is; awk -F"\t" '{ $1-=1;$2+=1}1' OFS='\t' file File 0623 0623 0624 0624 0643 0643 1059 1037 1037 1037 1038 1038... (2 Replies)
Discussion started by: pshields1984
2 Replies

2. Shell Programming and Scripting

ksh Arithmetic syntax error while comparing decimal numbers

Hello, I am having a problem when i execute following script on RHEL 6.4. Same script works fine on another machine where I have same version of RHEL and KSH. Below is the rpm and RHEL version. ossvm12(0)> rpm -qa | grep ksh ksh-20100621-19.el6.x86_64 ossvm12(0)> cat... (7 Replies)
Discussion started by: Adithya Gokhale
7 Replies

3. UNIX for Dummies Questions & Answers

Adding a column to a text file with row numbers

Hi, I would like to add a new column containing the row numbers to a text file. How do I go about doing that? Thanks! Example input: A X B Y C D Output: A X 1 B Y 2 C D 3 (5 Replies)
Discussion started by: evelibertine
5 Replies

4. UNIX for Dummies Questions & Answers

How to delete columns with numbers in an excel file?

Dear all, I have one file (see below) with more then 100 columns and 2500 rows, and need only column which has GType in label with Alphabets, please help me to remove these columns with numbers. input file is n.201.GType n-201.Theta n-201.R n_1.GType n_1.Theta n_1.R... (6 Replies)
Discussion started by: AAWT
6 Replies

5. Shell Programming and Scripting

Adding numbers

Hi I figured how to add my list of numbers. However how do I count so that after a certain number it lists the line. Example: 12 test1 46 test2 195 test3 174 test4 634 test5 185 test6 94 test7 So basically add the numbers and when the addition reaches 300 or less print the... (8 Replies)
Discussion started by: bombcan
8 Replies

6. Shell Programming and Scripting

the smallest number from 90% of highest numbers from all numbers in file

Hello All, I am having problem to find what is the smallest number from 90% of highest numbers from all numbers in file. I am having file with thousands of lines and hundreds of columns. I am familiar mainly with bash but I am open to whatever suggestion witch will lead to the solutions. If I... (11 Replies)
Discussion started by: Apfik
11 Replies

7. Shell Programming and Scripting

Arithmetic calculation on real numbers in Bourne Shell Script

I am begining to learn bourne shell and as a practice I have written a script which when given the purchase price and percentage of discount calculates the savings. I somehow cannot figure out why my script fails to do arthimatic calculation on real numbers. Could anyone look at the script... (5 Replies)
Discussion started by: Tirmazi
5 Replies

8. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies

9. UNIX for Dummies Questions & Answers

Adding Numbers in a line from a file

Hello everyone ! Ive searched everywhere and still havnt found enough information to help me overcome (what seems like) a small problem I have created a temporary file in which i store numbers which a seperated by a space, eg) 5 10 46 23 866 392 i wish to take the numbers for each line... (2 Replies)
Discussion started by: healwithchaos
2 Replies

10. Shell Programming and Scripting

Adding 2 numbers

I would like to write a script with BASH to add two numbers (integer) and write the result to the standard output. Please help! (7 Replies)
Discussion started by: Viper01
7 Replies
Login or Register to Ask a Question