Joining two csv files on common column and removing the second last column

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











up vote
1
down vote

favorite












I have two csv files:



file1:



C1, 1, 0, 1, 0, 1
C2, 1, 0, 1, 1, 0
C3, 0, 0, 1, 1, 0


file2:



C3, 1.2
C1, 2.3
C2, 1.8


I want to merge these two files based on C column (which produces):



C1, 1, 0, 1, 0, 1, 2.3
C2, 1, 0, 1, 1, 0, 1.8
C3, 0, 0, 1, 1, 0, 1.2


And then remove the second last column (to produce):



C1, 1, 0, 1, 0, 2.3
C2, 1, 0, 1, 1, 1.8
C3, 0, 0, 1, 1, 1.2









share|improve this question





















  • What did you try? Post your own efforts to the question
    – Inian
    yesterday










  • @Inian My try was this: (1) sort two files based on the common column, (2) apply join on this column (3) then awk all the columns except the second last.
    – Coder
    yesterday














up vote
1
down vote

favorite












I have two csv files:



file1:



C1, 1, 0, 1, 0, 1
C2, 1, 0, 1, 1, 0
C3, 0, 0, 1, 1, 0


file2:



C3, 1.2
C1, 2.3
C2, 1.8


I want to merge these two files based on C column (which produces):



C1, 1, 0, 1, 0, 1, 2.3
C2, 1, 0, 1, 1, 0, 1.8
C3, 0, 0, 1, 1, 0, 1.2


And then remove the second last column (to produce):



C1, 1, 0, 1, 0, 2.3
C2, 1, 0, 1, 1, 1.8
C3, 0, 0, 1, 1, 1.2









share|improve this question





















  • What did you try? Post your own efforts to the question
    – Inian
    yesterday










  • @Inian My try was this: (1) sort two files based on the common column, (2) apply join on this column (3) then awk all the columns except the second last.
    – Coder
    yesterday












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have two csv files:



file1:



C1, 1, 0, 1, 0, 1
C2, 1, 0, 1, 1, 0
C3, 0, 0, 1, 1, 0


file2:



C3, 1.2
C1, 2.3
C2, 1.8


I want to merge these two files based on C column (which produces):



C1, 1, 0, 1, 0, 1, 2.3
C2, 1, 0, 1, 1, 0, 1.8
C3, 0, 0, 1, 1, 0, 1.2


And then remove the second last column (to produce):



C1, 1, 0, 1, 0, 2.3
C2, 1, 0, 1, 1, 1.8
C3, 0, 0, 1, 1, 1.2









share|improve this question













I have two csv files:



file1:



C1, 1, 0, 1, 0, 1
C2, 1, 0, 1, 1, 0
C3, 0, 0, 1, 1, 0


file2:



C3, 1.2
C1, 2.3
C2, 1.8


I want to merge these two files based on C column (which produces):



C1, 1, 0, 1, 0, 1, 2.3
C2, 1, 0, 1, 1, 0, 1.8
C3, 0, 0, 1, 1, 0, 1.2


And then remove the second last column (to produce):



C1, 1, 0, 1, 0, 2.3
C2, 1, 0, 1, 1, 1.8
C3, 0, 0, 1, 1, 1.2






shell-script awk join merge






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked yesterday









Coder

1106




1106











  • What did you try? Post your own efforts to the question
    – Inian
    yesterday










  • @Inian My try was this: (1) sort two files based on the common column, (2) apply join on this column (3) then awk all the columns except the second last.
    – Coder
    yesterday
















  • What did you try? Post your own efforts to the question
    – Inian
    yesterday










  • @Inian My try was this: (1) sort two files based on the common column, (2) apply join on this column (3) then awk all the columns except the second last.
    – Coder
    yesterday















What did you try? Post your own efforts to the question
– Inian
yesterday




What did you try? Post your own efforts to the question
– Inian
yesterday












@Inian My try was this: (1) sort two files based on the common column, (2) apply join on this column (3) then awk all the columns except the second last.
– Coder
yesterday




@Inian My try was this: (1) sort two files based on the common column, (2) apply join on this column (3) then awk all the columns except the second last.
– Coder
yesterday










2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










You just have create a hash-map on the second file on the C column and use that on the first file as below. The actions right next FNR==NR applies to the first file specified at the end and the subsequent action happens on the last file. This is because of the special variables in awk, FNR and NR which track line numbers per file and across the files respectively.



awk -v FS="," -v OFS="," 'FNR==NR unique[$1]=$2; next $1 in unique $NF=unique[$1]; 1' file2 file1





share|improve this answer




















  • This works: awk -v FS="," -v OFS="," 'FNR==NR unique[$1]=$2; next $1 in unique $NF=unique[$1]"," $NF; 1' file2 file1 (It prints that second last column after merging as the last column.)
    – Coder
    yesterday


















up vote
1
down vote













Try also



join -t, -o1.1,1.2,1.3,1.4,1.5,2.2 <(sort file1) <(sort file2)





share|improve this answer




















  • It will work. But, when I have more columns, it has to be something else. Can you guide?
    – Coder
    yesterday










  • Be more specific.
    – RudiC
    yesterday










  • let us say file 1 has 100 columns and file 2 has 20.
    – Coder
    23 hours ago










  • While in principle that might be doable it will be quite some typing effort. Did you consider a cut approach?
    – RudiC
    18 hours ago










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: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
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%2f481855%2fjoining-two-csv-files-on-common-column-and-removing-the-second-last-column%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










You just have create a hash-map on the second file on the C column and use that on the first file as below. The actions right next FNR==NR applies to the first file specified at the end and the subsequent action happens on the last file. This is because of the special variables in awk, FNR and NR which track line numbers per file and across the files respectively.



awk -v FS="," -v OFS="," 'FNR==NR unique[$1]=$2; next $1 in unique $NF=unique[$1]; 1' file2 file1





share|improve this answer




















  • This works: awk -v FS="," -v OFS="," 'FNR==NR unique[$1]=$2; next $1 in unique $NF=unique[$1]"," $NF; 1' file2 file1 (It prints that second last column after merging as the last column.)
    – Coder
    yesterday















up vote
1
down vote



accepted










You just have create a hash-map on the second file on the C column and use that on the first file as below. The actions right next FNR==NR applies to the first file specified at the end and the subsequent action happens on the last file. This is because of the special variables in awk, FNR and NR which track line numbers per file and across the files respectively.



awk -v FS="," -v OFS="," 'FNR==NR unique[$1]=$2; next $1 in unique $NF=unique[$1]; 1' file2 file1





share|improve this answer




















  • This works: awk -v FS="," -v OFS="," 'FNR==NR unique[$1]=$2; next $1 in unique $NF=unique[$1]"," $NF; 1' file2 file1 (It prints that second last column after merging as the last column.)
    – Coder
    yesterday













up vote
1
down vote



accepted







up vote
1
down vote



accepted






You just have create a hash-map on the second file on the C column and use that on the first file as below. The actions right next FNR==NR applies to the first file specified at the end and the subsequent action happens on the last file. This is because of the special variables in awk, FNR and NR which track line numbers per file and across the files respectively.



awk -v FS="," -v OFS="," 'FNR==NR unique[$1]=$2; next $1 in unique $NF=unique[$1]; 1' file2 file1





share|improve this answer












You just have create a hash-map on the second file on the C column and use that on the first file as below. The actions right next FNR==NR applies to the first file specified at the end and the subsequent action happens on the last file. This is because of the special variables in awk, FNR and NR which track line numbers per file and across the files respectively.



awk -v FS="," -v OFS="," 'FNR==NR unique[$1]=$2; next $1 in unique $NF=unique[$1]; 1' file2 file1






share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









Inian

3,745823




3,745823











  • This works: awk -v FS="," -v OFS="," 'FNR==NR unique[$1]=$2; next $1 in unique $NF=unique[$1]"," $NF; 1' file2 file1 (It prints that second last column after merging as the last column.)
    – Coder
    yesterday

















  • This works: awk -v FS="," -v OFS="," 'FNR==NR unique[$1]=$2; next $1 in unique $NF=unique[$1]"," $NF; 1' file2 file1 (It prints that second last column after merging as the last column.)
    – Coder
    yesterday
















This works: awk -v FS="," -v OFS="," 'FNR==NR unique[$1]=$2; next $1 in unique $NF=unique[$1]"," $NF; 1' file2 file1 (It prints that second last column after merging as the last column.)
– Coder
yesterday





This works: awk -v FS="," -v OFS="," 'FNR==NR unique[$1]=$2; next $1 in unique $NF=unique[$1]"," $NF; 1' file2 file1 (It prints that second last column after merging as the last column.)
– Coder
yesterday













up vote
1
down vote













Try also



join -t, -o1.1,1.2,1.3,1.4,1.5,2.2 <(sort file1) <(sort file2)





share|improve this answer




















  • It will work. But, when I have more columns, it has to be something else. Can you guide?
    – Coder
    yesterday










  • Be more specific.
    – RudiC
    yesterday










  • let us say file 1 has 100 columns and file 2 has 20.
    – Coder
    23 hours ago










  • While in principle that might be doable it will be quite some typing effort. Did you consider a cut approach?
    – RudiC
    18 hours ago














up vote
1
down vote













Try also



join -t, -o1.1,1.2,1.3,1.4,1.5,2.2 <(sort file1) <(sort file2)





share|improve this answer




















  • It will work. But, when I have more columns, it has to be something else. Can you guide?
    – Coder
    yesterday










  • Be more specific.
    – RudiC
    yesterday










  • let us say file 1 has 100 columns and file 2 has 20.
    – Coder
    23 hours ago










  • While in principle that might be doable it will be quite some typing effort. Did you consider a cut approach?
    – RudiC
    18 hours ago












up vote
1
down vote










up vote
1
down vote









Try also



join -t, -o1.1,1.2,1.3,1.4,1.5,2.2 <(sort file1) <(sort file2)





share|improve this answer












Try also



join -t, -o1.1,1.2,1.3,1.4,1.5,2.2 <(sort file1) <(sort file2)






share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









RudiC

3,0411211




3,0411211











  • It will work. But, when I have more columns, it has to be something else. Can you guide?
    – Coder
    yesterday










  • Be more specific.
    – RudiC
    yesterday










  • let us say file 1 has 100 columns and file 2 has 20.
    – Coder
    23 hours ago










  • While in principle that might be doable it will be quite some typing effort. Did you consider a cut approach?
    – RudiC
    18 hours ago
















  • It will work. But, when I have more columns, it has to be something else. Can you guide?
    – Coder
    yesterday










  • Be more specific.
    – RudiC
    yesterday










  • let us say file 1 has 100 columns and file 2 has 20.
    – Coder
    23 hours ago










  • While in principle that might be doable it will be quite some typing effort. Did you consider a cut approach?
    – RudiC
    18 hours ago















It will work. But, when I have more columns, it has to be something else. Can you guide?
– Coder
yesterday




It will work. But, when I have more columns, it has to be something else. Can you guide?
– Coder
yesterday












Be more specific.
– RudiC
yesterday




Be more specific.
– RudiC
yesterday












let us say file 1 has 100 columns and file 2 has 20.
– Coder
23 hours ago




let us say file 1 has 100 columns and file 2 has 20.
– Coder
23 hours ago












While in principle that might be doable it will be quite some typing effort. Did you consider a cut approach?
– RudiC
18 hours ago




While in principle that might be doable it will be quite some typing effort. Did you consider a cut approach?
– RudiC
18 hours ago

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f481855%2fjoining-two-csv-files-on-common-column-and-removing-the-second-last-column%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay