Home > bash > using join to merge files

using join to merge files

$cat file1

2011-03-01,0
2011-03-02,0
2011-03-03,0
2011-03-04,0
2011-03-05,0

$cat file2

2011-03-01,13400
2011-03-02,1730
2011-03-05,7593

To merge the two files so that the final file has all the rows in file1 use

$join -t’,’ -1 1 -2 1 -a 1 -e ‘0’ -o ‘1.1,2.2’ file1 file2

2011-03-01,13400
2011-03-02,1730
2011-03-03,0
2011-03-04,0
2011-03-05,7593

An excerpt from info join

-a FILENUM
print unpairable lines coming from file FILENUM, where FILENUM
is 1 or 2, corresponding to FILE1 or FILE2

-e EMPTY
replace missing input fields with EMPTY

-o FORMAT
obey FORMAT while constructing output line

-t CHAR
use CHAR as input and output field separator

-1 FIELD
join on this FIELD of file 1

-2 FIELD
join on this FIELD of file 2

Advertisements
Categories: bash Tags:
  1. No comments yet.
  1. February 17, 2016 at 6:37 am

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