Computing.Net > Forums > Programming > Compare 2 files and merge in awk

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Compare 2 files and merge in awk

Reply to Message Icon

Name: newlinux
Date: December 11, 2007 at 07:20:05 Pacific
OS: Scientific Linux
CPU/Ram: Quad- 8GB
Product: Dell
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: klint
Date: December 11, 2007 at 16:16:27 Pacific
Reply:

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.)


0
Reply to Message Icon

Related Posts

See More


Slight inaccuracy Roundin... append creation date to f...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Compare 2 files and merge in awk

How to compare 2 files using awk? www.computing.net/answers/programming/how-to-compare-2-files-using-awk/15598.html

How to compare 2 file date time stamps in bat www.computing.net/answers/programming/how-to-compare-2-file-date-time-stamps-in-bat/19411.html

Enumerate all files and folders www.computing.net/answers/programming/enumerate-all-files-and-folders/9618.html