Computing.Net > Forums > Programming > Compare files with 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 files with AWK

Reply to Message Icon

Name: hetwich
Date: March 26, 2007 at 04:41:56 Pacific
OS: UNIX
CPU/Ram: Unknown
Product: HP
Comment:

Hallo,
We want to compare two files. The first two fields are key values.
Example:

file 1
------
A; 1234, 12
B; 4435; 24
C; 7777; 50

file 2
------
A; 1234, 32
B; 5435; 24
C; 7777; 50
D; 8888; 50

Result
------
A; 1234; 12; Updated 32
B; 5435; 24; New in F1
D; 8888; 50; New in F2


When the first two fields are the same but the third value differs, this should be marked as update. If first two fields are not present in second file this shoud be marked as new in File 1 (vise versa).

This problem can be solved in AWK but I am not familiar with this tool.

King regards
Hetwich



Sponsored Link
Ads by Google

Response Number 1
Name: cup
Date: March 26, 2007 at 10:56:09 Pacific
Reply:

Why not just use diff


0

Response Number 2
Name: Phil Perry
Date: March 26, 2007 at 17:39:46 Pacific
Reply:

It might be possible to do this with AWK, but your description is incomplete. Are the field separators always ; ? I see a , in there too, which makes it difficult to use AWK's field separator. Are there blanks between each field, which could be used as the field separator?

If D;8888;50 is "new in F2", why isn't B;5435;24 also "new in F2" (and B;4435;24 "new in F1")? Your results don't seem to match your description. I only have experience doing fairly simple stuff in AWK, so I don't think I could get this thing working in AWK.

As cup suggested, "diff" might be the place to start. It will ignore unchanged lines, and flag lines inserted or deleted going from one file to the other. Then you post-process the diff file to determine whether a paired insertion/deletion meets the criteria for "updated" rather than "new in X". AWK might be able to handle that, or a Q&D C program might be easier.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







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 files with AWK

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

Editing .ini file with a batch file www.computing.net/answers/programming/editing-ini-file-with-a-batch-file/12127.html

help with awk - sed www.computing.net/answers/programming/help-with-awk-sed/17119.html