Compare 2 files and merge in awk

Dell
December 11, 2007 at 07:20:05
Specs: Scientific Linux, Quad- 8GB
Hello!
Very new to awk I need to do the following...
I have one file looking like this:
Probe "AFX_1234_at1"
Probe "AFX_554545_at1"
Probe "AFX_34342_at2"

The I have another file looking like that:
Probe_set "AFX_1234_at"
Signal "AFX_3336" 125

Probe_set "AFX_554545_at"
Signal "AFX_3336" 554

Probe_set "AFX_34342_at"
Signal "AFX_3336" 88.2

And I would need to build a file looking like this:
Probe "AFX_1234_at1"
Signal "AFX_3336" 125

Probe "AFX_554545_at1"
Signal "AFX_3336" 554

Probe "AFX_34342_at2"
Signal "AFX_3336" 88.2

In other words, I need to have the Probe_set of file 2 replaced by the correponding Probe from file 1, and I need to keep the Signal line.

Thanks a lot for your inputs!

newtoallthis


See More: Compare 2 files and merge in awk

Report •


#1
December 11, 2007 at 16:16:27
A very rough sketch:

1. In your BEGIN block, open and read the first file. Create a dictionary (associative array) by splitting e.g. "AFX_1234_at1" (using substring functions) into "AFX_1234_at" and "1", and put these into an associative array like probe["AFX_1234_at"] = "1".

2. Treat the second file as your main input.

/Probe_set/ {
print "Probe" $1 probe[$1]
}
/Signal/ {
print
}

(I haven't tested the above code; you'll have to try it out yourself and fix it.)


Report •
Related Solutions


Ask Question