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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Merging 2 files and replacing the content of the old 1

Status
Not open for further replies.

wotaskd

Technical User
Jan 27, 2009
2
US
I'm trying to compare two files (OLD, NEW) using their first column as the index. Each line corresponds to a record. I'd like to create a third file (FINAL) that has keeps all the records that are present on the OLD and NEW files, but at the same time updates the remaining fields in such a way that NEW replaces OLD. This is an example of what it would look like:

OLD
1001 aaaa 44
1002 bbbb 55
1003 cccc 66
1004 dddd 77

NEW
1002 bbbb 88
1003 cccc 11
1004 ffff 22
1005 hhhh 99

FINAL
1001 aaaa 44
1002 bbbb 88
1003 cccc 11
1004 ffff 22
1005 hhhh 99

Tried doing this with other tools but was not able to. Any idea of how to do this in awk?

Thanks!

wotaskd
 
you may try something like this:
awk '{a[$1]=$0}END{for(i in a)print a}' OLD NEW | sort > FINAL

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top