Monday, April 20, 2009

Merging 2 files using awk

When the contents of two files are required to be merged without repetition and neatly sorted AWK can help us do it on the fly.
The comparison of two files is done using the first column as parameter. e.g. there are two files as below with required ouput:

file1

827 111111111888811117122111
828 111189292929222222222222
830 112203030371818181811111
832 120811117777777777777777
833 110000000000000000811111

file2

829 561111111888811117122111
830 112203030371818181811111
831 199991772929222222222222
833 110000000000000000811111
834 166666666666666666777777

Merged_file

827 111111111888811117122111
828 111189292929222222222222
829 561111111888811117122111
830 112203030371818181811111
831 199991772929222222222222
832 120811117777777777777777
833 110000000000000000811111
834 166666666666666666777777

Here's the code:

# awk '{a[$1]=$0}END{for(i in a)print a[i]}' file1 file2 | sort > Merged_file

No comments:

Post a Comment