Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

comparing 2 files and changing 1

Status
Not open for further replies.

Zodiak99

ISP
Oct 26, 2001
15
AT
Hi awk gurus,

my wife has asked me if I could program something for her, but my last experience of working with sed and awk dates back to like 1995.
Perl would be another option I guess to accomplish this task but I have a feeling it might be even easier in awk.

But let me explain the problem:

I have 2 files, and based on specific information in file1 I have to change the contents of file2.

Here is how the files look like:

file1:
[tt] 5 Val HG1# 47 Ile HB 2.8 4.5
7 Val HB 8 Ser HB2 2.8 4.5
7 Val HG2# 8 Ser Hn 2.8 4.5
8 Ser HA 48 Ile HD1# 2.8 6.0
[/tt]
file2:
[tt]! residue 1 Met , NOEs forward in sequence
! residue 2 Ala , NOEs forward in sequence
! residue 3 Ser , NOEs forward in sequence
! residue 4 Gly , NOEs forward in sequence
assi (resi 4 and name HA1 ) (resi 5 and name HN ) 2.80 2.80 0.00
assi (resi 4 and name HA2 ) (resi 5 and name HN ) 2.80 2.80 0.00
assi (resi 4 and name HA2 ) (resi 6 and name HN ) 4.50 4.50 0.00
! residue 5 Val , NOEs forward in sequence
assi (resi 5 and name HN ) (resi 6 and name HN ) 2.80 2.80 0.00
assi (resi 5 and name HN ) (resi 6 and name HA ) 4.50 4.50 0.00
assi (resi 5 and name HN ) (resi 6 and name HB# ) 4.50 4.50 0.00[/tt]

Ok, I can use the default FS and get nice fields.
The information in file1 for finding matching lines in file2 are fields
1,3,4 and 6.

In file2 we are ignoring all lines starting with a ! and we only look at fields
3,6,9 and 12 to dinf matches.

Now I want to search for all lines where
[tt]
field 1(file1) matches field3(file2)
field 3(file1) matches field6(file2)
field 4(file1) matches field9(file2)
field 6(file1) matches field12(file2)
[/tt]
In those matching lines I need to replace the contents of
field 13 and 14 in file2 with the value of field 8 of file1.
Optional: additional checking that field 13 and 14 of file2
should be equal to field 8 of file1.

Ok, I guess that was confusing enough but I tried to explain
as well as I could.

I have tried several approaches but failed misarably :-(
Maybe someone with more programming experience could get me
started? Currently my wife is doing all this by hand which
takes hours - file1 can be several hunderd lines and file2
1000+ lines.

Thanks alot in advance for all hints - if you get me started
I guess I might be able to figure out the rest by myself ;-)

Zodiak
 
Hi Zodiak99,

You could try this. It's not tested, but I think it matches what you requested:

% cat mywifes.awk
#!/usr/bin/awk -f
BEGIN{
while ( (getline < &quot;file1&quot; ) > 0 )
{
array[$1,$3,$4,$6]=$8;
}
close(&quot;file1&quot;);
}

$1 !~ /!/ && array[$3,$6,$9,$12] != &quot;&quot; {
$13 = array[$3,$6,$9,$12];
$14 = array[$3,$6,$9,$12];
}

To run it:
mywifes.awk file2

Hope this helps,
Grant
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top