LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-23-2008, 12:22 AM   #1
raghu123
Member
 
Registered: May 2008
Posts: 34

Rep: Reputation: 15
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......
 
Old 08-23-2008, 01:11 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
how about showing your code?
 
Old 08-23-2008, 02:11 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Code:
awk 'BEGIN { i=1 } !/^TER/ { $2=i; $6=i; print; i++; } /^TER/ { i=1; print; }'
 
Old 08-23-2008, 02:36 AM   #4
raghu123
Member
 
Registered: May 2008
Posts: 34

Original Poster
Rep: Reputation: 15
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.........
 
Old 08-23-2008, 07:07 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 08-23-2008, 07:12 AM   #6
raghu123
Member
 
Registered: May 2008
Posts: 34

Original Poster
Rep: Reputation: 15
Sorry matthew........tat reply was to ghostdog.....not to you.....

Anyways thanks for your code.....
 
Old 08-25-2008, 12:27 AM   #7
raghu123
Member
 
Registered: May 2008
Posts: 34

Original Poster
Rep: Reputation: 15
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...
 
Old 08-25-2008, 01:51 AM   #8
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Code:
awk 'BEGIN { i=1;j=1 } !/^TER/ { $2=j; $6=i; print; i++;j++ } /^TER/ { i=1; print; }'
 
Old 08-25-2008, 02:27 AM   #9
raghu123
Member
 
Registered: May 2008
Posts: 34

Original Poster
Rep: Reputation: 15
Smile

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
File serial numbers exvor Programming 4 07-20-2007 04:57 PM
How to get serial numbers? G00fy Programming 8 07-19-2007 03:04 PM
bash scripting: loop over a file, replacing two decimal numbers frankie_DJ Programming 2 04-30-2007 04:04 PM
colour selected columns using php sessions? ati Programming 1 05-10-2006 08:30 AM
Doing arithmetics on columns of numbers hhegab Programming 2 03-08-2006 10:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration