LinuxQuestions.org
Help answer threads with 0 replies.
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 03-20-2011, 08:04 PM   #1
vjramana
Member
 
Registered: Sep 2009
Posts: 89

Rep: Reputation: 0
Replacing columns in a file


I am doing some operation on a file which represents a molecular system in PDB file format.

The format of the file which I am operating now is as below and this represents only one PDB file:

Code:
REMARKS
ATOM      1  O22 DDM     1       5.844   4.178   7.821  0.00  0.00
ATOM      2  H62 DDM     1       6.423   4.054   8.578  0.00  0.00
ATOM      3  C22 DDM     1       6.606   4.907   6.827  0.00  0.00
TER
ATOM    163  O22 DDM     3       3.758  10.266   8.321  0.00  0.00
ATOM    164  H62 DDM     3       4.450  10.757   8.770  0.00  0.00
ATOM    165  C22 DDM     3       3.346  10.797   7.028  0.00  0.00
TER
ATOM    325  O22 DDM     5       8.310   1.239  14.788  0.00  0.00
ATOM    326  H62 DDM     5       8.858   0.914  15.507  0.00  0.00
ATOM    327  C22 DDM     5       9.143   2.061  13.825  0.00  0.00
TER
END
Basically I want to change the sequence of the numbers in the second and fifth column.
To achive this I generated two seperate files which contain the number in the sequence of.

Quote:
1
2
3

4
5
6

7
8
9
and

Quote:
1
1
1

2
2
2

3
3
3
Now I am trying to replace the first sequence in the second column and the second sequence in the fifth column and finally get the format as below:

Code:
REMARKS
ATOM      1  O22 DDM     1       5.844   4.178   7.821  0.00  0.00
ATOM      2  H62 DDM     1       6.423   4.054   8.578  0.00  0.00
ATOM      3  C22 DDM     1       6.606   4.907   6.827  0.00  0.00
TER
ATOM      4  O22 DDM     2       3.758  10.266   8.321  0.00  0.00
ATOM      5  H62 DDM     2       4.450  10.757   8.770  0.00  0.00
ATOM      6  C22 DDM     2       3.346  10.797   7.028  0.00  0.00
TER
ATOM      7  O22 DDM     3       8.310   1.239  14.788  0.00  0.00
ATOM      8  H62 DDM     3       8.858   0.914  15.507  0.00  0.00
ATOM      9  C22 DDM     3       9.143   2.061  13.825  0.00  0.00
TER
END

I want to do this for each PDB file. But I need some help on how to replace this columns file after file.

I really appreciate any help to achieve this.

Vijay
 
Old 03-20-2011, 10:53 PM   #2
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 101Reputation: 101
Something like this:
Code:
$ paste 1.txt 2.txt input.txt | awk '{ print $3,$1,$5,$6,$2,$8,$9,$10,$11,$12 }'
but you should use printf to keep the formatting as original file.
 
1 members found this post helpful.
Old 03-21-2011, 07:30 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Code:
awk 'FNR==1{i++;j=0}FILENAME != "input.txt"{if(!/^$/)_[i,j++]=$0;next}/ATOM/{$2 = _[1,j];$5 = _[2,j++]}1' 1.txt 2.txt input.txt
 
1 members found this post helpful.
  


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
how to cut two columns in data file and print file secondchanti Linux - Newbie 6 07-16-2010 06:01 AM
How to compare two columns in a file. shilpa.godhe Linux - Newbie 2 03-29-2010 02:42 AM
Replacing selected columns by Serial numbers incremently raghu123 Programming 8 08-25-2008 02:27 AM
how to append columns form a column file in another file adam_blackice Programming 4 09-17-2007 11:33 PM

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

All times are GMT -5. The time now is 04:11 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