LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Replacing selected columns by Serial numbers incremently (https://www.linuxquestions.org/questions/programming-9/replacing-selected-columns-by-serial-numbers-incremently-664750/)

raghu123 08-23-2008 12:22 AM

Replacing selected columns by Serial numbers incremently
 
Hi all,

I have a file like this:

ATOM 2 CA GLU A 76 -1.924 17.878 -5.440 1.00 51.41
ATOM 11 CA PRO A 77 -2.544 20.862 -2.979 1.00 37.40
ATOM 18 CA GLU A 78 -6.174 21.593 -2.142 1.00 30.40
ATOM 27 CA TRP A 79 -7.750 24.746 -3.511 1.00 17.67
TER
ATOM 2 CA THR G 1 28.430 63.361 51.737 1.00 42.22
ATOM 9 CA PRO G 2 26.967 63.584 55.274 1.00 31.31
ATOM 16 CA GLN G 3 29.684 62.824 57.805 1.00 33.82
ATOM 25 CA ASN G 4 27.515 61.696 60.704 1.00 26.00
ATOM 33 CA ILE G 5 24.191 60.162 61.569 1.00 20.73
ATOM 41 CA THR G 6 22.747 63.483 62.717 1.00 25.06
ATOM 48 CA ASP G 7 23.391 65.211 59.400 1.00 28.96
ATOM 56 CA LEU G 8 22.227 62.164 57.477 1.00 25.58
ATOM 64 CA CYS G 9 19.067 61.936 59.557 1.00 22.32
ATOM 70 CA ALA G 10 18.422 65.589 58.785 1.00 27.72
ATOM 75 CA GLU G 11 18.011 64.885 55.069 1.00 26.90
ATOM 84 CA TYR G 12 14.628 63.226 55.416 1.00 26.27
TER
ATOM 2 CA LYS A 865 50.023 37.415 38.084 1.00102.35
ATOM 11 CA ASN A 866 47.964 34.458 39.220 1.00 99.96
ATOM 19 CA LEU A 867 48.644 30.875 40.293 1.00 96.60
ATOM 27 CA ASP A 868 49.443 28.917 37.158 1.00 92.43
TER

I want the 2nd field i.e field having numbers 2, 11, 18, 27 so on to be replaced by 1, 2, 3 incremently til the last line of the file but excluding those lines having TER. I mean starting with the 1st line i.e ATOM 2, it shoud start with replacing the 2nd fields by Sl. numb incremently excluding those lines having TER in them.

Similarly, i wan the 6th field, i.e again number column to start with one and increments til TER is found......TER refrers to termination of that substance.....so next, again the field shoud increment, for eg. in the 3rd substance the line starts with
ATOM 2 CA LYS A 865 50.023 37.415 38.084 1.00102.35....
Here 865 shud b relaced by one and shud increment in the same field/column til TER comes.......

Please help me........i am in great need for this......

ghostdog74 08-23-2008 01:11 AM

how about showing your code?

matthewg42 08-23-2008 02:11 AM

Code:

awk 'BEGIN { i=1 } !/^TER/ { $2=i; $6=i; print; i++; } /^TER/ { i=1; print; }'

raghu123 08-23-2008 02:36 AM

I am using a different approach, but its not yielding me the perfect o/p..

i am using cut, grep and loops for the particular fields by using pipes.....but wat i wan s for 2 of my fields and also numbering not for TER line.....so.........

matthewg42 08-23-2008 07:07 AM

What's wrong with the awk approach? It's almost certainly faster than a combination of a loop in shell script with cuts, greps and so on all over the place.

raghu123 08-23-2008 07:12 AM

Sorry matthew........tat reply was to ghostdog.....not to you.....

Anyways thanks for your code.....

raghu123 08-25-2008 12:27 AM

Hi matthew....
The awk code that u gave is incrementing the 2nd field in the same way of the 6th field.....
I want the 2nd field to be incremented from 1 to the last line (1 to 34689 in my file) excluding the TER line.....i mean it shoud not count the line having "TER"....

The code is doing good with the 6th field. i.e incrementing til TER and again beginning with 1 till TER is found, but this is not what i wanted in the 2nd field...

Mr. C. 08-25-2008 01:51 AM

Code:

awk 'BEGIN { i=1;j=1 } !/^TER/ { $2=j; $6=i; print; i++;j++ } /^TER/ { i=1; print; }'

raghu123 08-25-2008 02:27 AM

THanks a lot.......its workin fine!!!


All times are GMT -5. The time now is 02:42 AM.