Skip to content

Joining files

June 10, 2011

One of my friend pinged me and gave a good problem (according to me)  to solve

Wrote the following lines

join -a1 -a2 -1 1 -2 1 -o 0 1.2 1.3 2.3 2.2 -e “0” file1.txt file2.txt  > r.txt
gawk ‘BEGIN { FS=” “} {print $1 ” ” (($2 == 0) ? $5 : $2) ” ” $3 ” ” $4}’ r.txt

and solved the problem

And, need to optimize….

The problem statement is

file-1

A B 1

X Y 3

file-2

A B 4

W E 4

the result should be

A B 1 4

X Y 3 0

W E 4 0

Got a good reference for joins here.

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: