Join two files based on a column

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












file1:



a, 1 
b, 5
c, 2
f, 7


file2:



a, 2 
f, 9
g, 3


I want to join file 1 and file 2 based on column 1 and get file 3 as below.



file3:



a, 1, 2 
b, 5, -
c, 2, -
f, 7, 9
g, -, 3


merge the matching values and also keep the specific ones from each file










share|improve this question



















  • 1




    what have you tried to solve this? see unix.stackexchange.com/questions/43417/… and unix.stackexchange.com/questions/122919/… for reference...
    – Sundeep
    Oct 4 '17 at 5:02














up vote
3
down vote

favorite












file1:



a, 1 
b, 5
c, 2
f, 7


file2:



a, 2 
f, 9
g, 3


I want to join file 1 and file 2 based on column 1 and get file 3 as below.



file3:



a, 1, 2 
b, 5, -
c, 2, -
f, 7, 9
g, -, 3


merge the matching values and also keep the specific ones from each file










share|improve this question



















  • 1




    what have you tried to solve this? see unix.stackexchange.com/questions/43417/… and unix.stackexchange.com/questions/122919/… for reference...
    – Sundeep
    Oct 4 '17 at 5:02












up vote
3
down vote

favorite









up vote
3
down vote

favorite











file1:



a, 1 
b, 5
c, 2
f, 7


file2:



a, 2 
f, 9
g, 3


I want to join file 1 and file 2 based on column 1 and get file 3 as below.



file3:



a, 1, 2 
b, 5, -
c, 2, -
f, 7, 9
g, -, 3


merge the matching values and also keep the specific ones from each file










share|improve this question















file1:



a, 1 
b, 5
c, 2
f, 7


file2:



a, 2 
f, 9
g, 3


I want to join file 1 and file 2 based on column 1 and get file 3 as below.



file3:



a, 1, 2 
b, 5, -
c, 2, -
f, 7, 9
g, -, 3


merge the matching values and also keep the specific ones from each file







awk join merge






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 4 '17 at 5:00









Sundeep

6,9611826




6,9611826










asked Oct 4 '17 at 4:56









chris

161




161







  • 1




    what have you tried to solve this? see unix.stackexchange.com/questions/43417/… and unix.stackexchange.com/questions/122919/… for reference...
    – Sundeep
    Oct 4 '17 at 5:02












  • 1




    what have you tried to solve this? see unix.stackexchange.com/questions/43417/… and unix.stackexchange.com/questions/122919/… for reference...
    – Sundeep
    Oct 4 '17 at 5:02







1




1




what have you tried to solve this? see unix.stackexchange.com/questions/43417/… and unix.stackexchange.com/questions/122919/… for reference...
– Sundeep
Oct 4 '17 at 5:02




what have you tried to solve this? see unix.stackexchange.com/questions/43417/… and unix.stackexchange.com/questions/122919/… for reference...
– Sundeep
Oct 4 '17 at 5:02










1 Answer
1






active

oldest

votes

















up vote
4
down vote













Using join:



$ join -t, -a 1 -a 2 -o0,1.2,2.2 -e ' -' file1 file2
a, 1, 2
b, 5, -
c, 2, -
f, 7, 9
g, -, 3


The standard join utility will perform a relational JOIN operation on the two sorted input files.



The flags used here tells the utility to expect comma-delimited input (-t,) and to produce output for all entries in both files (-a 1 -a 2, otherwise it would only produce output for lines with matching first field). We then ask for the join field along with the second column of both files to be outputted (-o0,1.2,2.2) and say that any missing field should be replaced by the string ␣- (space-dash, with -e ' -').



If the input is not sorted, it has to be pre-sorted. In shells that understands process substitution with <( ... ), this my be done through



join -t, -a 1 -a 2 -o0,1.2,2.2 -e ' -' <( sort file1 ) <( sort file2 )





share|improve this answer




















  • I didn't even know there is a join command, but it's part of the GNU coreutils – thanks a lot!
    – dessert
    Oct 4 '17 at 6:55











  • @dessert It's a standard command that should be available on all Unices.
    – Kusalananda
    Oct 4 '17 at 6:57










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f395961%2fjoin-two-files-based-on-a-column%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote













Using join:



$ join -t, -a 1 -a 2 -o0,1.2,2.2 -e ' -' file1 file2
a, 1, 2
b, 5, -
c, 2, -
f, 7, 9
g, -, 3


The standard join utility will perform a relational JOIN operation on the two sorted input files.



The flags used here tells the utility to expect comma-delimited input (-t,) and to produce output for all entries in both files (-a 1 -a 2, otherwise it would only produce output for lines with matching first field). We then ask for the join field along with the second column of both files to be outputted (-o0,1.2,2.2) and say that any missing field should be replaced by the string ␣- (space-dash, with -e ' -').



If the input is not sorted, it has to be pre-sorted. In shells that understands process substitution with <( ... ), this my be done through



join -t, -a 1 -a 2 -o0,1.2,2.2 -e ' -' <( sort file1 ) <( sort file2 )





share|improve this answer




















  • I didn't even know there is a join command, but it's part of the GNU coreutils – thanks a lot!
    – dessert
    Oct 4 '17 at 6:55











  • @dessert It's a standard command that should be available on all Unices.
    – Kusalananda
    Oct 4 '17 at 6:57














up vote
4
down vote













Using join:



$ join -t, -a 1 -a 2 -o0,1.2,2.2 -e ' -' file1 file2
a, 1, 2
b, 5, -
c, 2, -
f, 7, 9
g, -, 3


The standard join utility will perform a relational JOIN operation on the two sorted input files.



The flags used here tells the utility to expect comma-delimited input (-t,) and to produce output for all entries in both files (-a 1 -a 2, otherwise it would only produce output for lines with matching first field). We then ask for the join field along with the second column of both files to be outputted (-o0,1.2,2.2) and say that any missing field should be replaced by the string ␣- (space-dash, with -e ' -').



If the input is not sorted, it has to be pre-sorted. In shells that understands process substitution with <( ... ), this my be done through



join -t, -a 1 -a 2 -o0,1.2,2.2 -e ' -' <( sort file1 ) <( sort file2 )





share|improve this answer




















  • I didn't even know there is a join command, but it's part of the GNU coreutils – thanks a lot!
    – dessert
    Oct 4 '17 at 6:55











  • @dessert It's a standard command that should be available on all Unices.
    – Kusalananda
    Oct 4 '17 at 6:57












up vote
4
down vote










up vote
4
down vote









Using join:



$ join -t, -a 1 -a 2 -o0,1.2,2.2 -e ' -' file1 file2
a, 1, 2
b, 5, -
c, 2, -
f, 7, 9
g, -, 3


The standard join utility will perform a relational JOIN operation on the two sorted input files.



The flags used here tells the utility to expect comma-delimited input (-t,) and to produce output for all entries in both files (-a 1 -a 2, otherwise it would only produce output for lines with matching first field). We then ask for the join field along with the second column of both files to be outputted (-o0,1.2,2.2) and say that any missing field should be replaced by the string ␣- (space-dash, with -e ' -').



If the input is not sorted, it has to be pre-sorted. In shells that understands process substitution with <( ... ), this my be done through



join -t, -a 1 -a 2 -o0,1.2,2.2 -e ' -' <( sort file1 ) <( sort file2 )





share|improve this answer












Using join:



$ join -t, -a 1 -a 2 -o0,1.2,2.2 -e ' -' file1 file2
a, 1, 2
b, 5, -
c, 2, -
f, 7, 9
g, -, 3


The standard join utility will perform a relational JOIN operation on the two sorted input files.



The flags used here tells the utility to expect comma-delimited input (-t,) and to produce output for all entries in both files (-a 1 -a 2, otherwise it would only produce output for lines with matching first field). We then ask for the join field along with the second column of both files to be outputted (-o0,1.2,2.2) and say that any missing field should be replaced by the string ␣- (space-dash, with -e ' -').



If the input is not sorted, it has to be pre-sorted. In shells that understands process substitution with <( ... ), this my be done through



join -t, -a 1 -a 2 -o0,1.2,2.2 -e ' -' <( sort file1 ) <( sort file2 )






share|improve this answer












share|improve this answer



share|improve this answer










answered Oct 4 '17 at 6:15









Kusalananda

105k14209326




105k14209326











  • I didn't even know there is a join command, but it's part of the GNU coreutils – thanks a lot!
    – dessert
    Oct 4 '17 at 6:55











  • @dessert It's a standard command that should be available on all Unices.
    – Kusalananda
    Oct 4 '17 at 6:57
















  • I didn't even know there is a join command, but it's part of the GNU coreutils – thanks a lot!
    – dessert
    Oct 4 '17 at 6:55











  • @dessert It's a standard command that should be available on all Unices.
    – Kusalananda
    Oct 4 '17 at 6:57















I didn't even know there is a join command, but it's part of the GNU coreutils – thanks a lot!
– dessert
Oct 4 '17 at 6:55





I didn't even know there is a join command, but it's part of the GNU coreutils – thanks a lot!
– dessert
Oct 4 '17 at 6:55













@dessert It's a standard command that should be available on all Unices.
– Kusalananda
Oct 4 '17 at 6:57




@dessert It's a standard command that should be available on all Unices.
– Kusalananda
Oct 4 '17 at 6:57

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f395961%2fjoin-two-files-based-on-a-column%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)