How to compare two CSV files and display unique records? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionComparing two files in unix and awkSplit the file comparing two fields in the filesMerging two Unix filesCompare two a columns in two csv files and print to third fileCompare two files by first column. Keep rows if matchingJoin two csv files by matching columns, join commandMerging two files and creating a new one. file 1 has got more than 100 colmns and file 2 has got 2Linux Compare two files on different field and print field 1 of first fileCompare first and second column of two files and print the row from second file if there is a matchCompare CSV file with while read loop in bash
Should you tell Jews they are breaking a commandment?
Determine whether f is a function, an injection, a surjection
Is there a documented rationale why the House Ways and Means chairman can demand tax info?
Is above average number of years spent on PhD considered a red flag in future academia or industry positions?
What items from the Roman-age tech-level could be used to deter all creatures from entering a small area?
What did Darwin mean by 'squib' here?
Slither Like a Snake
Two different pronunciation of "понял"
Was credit for the black hole image misattributed?
What do you call a plan that's an alternative plan in case your initial plan fails?
How to say that you spent the night with someone, you were only sleeping and nothing else?
Can a zero nonce be safely used with AES-GCM if the key is random and never used again?
Stars Make Stars
I'm thinking of a number
Is it possible to ask for a hotel room without minibar/extra services?
Estimate capacitor parameters
How are presidential pardons supposed to be used?
Using "nakedly" instead of "with nothing on"
Simulating Exploding Dice
Active filter with series inductor and resistor - do these exist?
Replacing HDD with SSD; what about non-APFS/APFS?
What computer would be fastest for Mathematica Home Edition?
Did the new image of black hole confirm the general theory of relativity?
When is phishing education going too far?
How to compare two CSV files and display unique records?
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionComparing two files in unix and awkSplit the file comparing two fields in the filesMerging two Unix filesCompare two a columns in two csv files and print to third fileCompare two files by first column. Keep rows if matchingJoin two csv files by matching columns, join commandMerging two files and creating a new one. file 1 has got more than 100 colmns and file 2 has got 2Linux Compare two files on different field and print field 1 of first fileCompare first and second column of two files and print the row from second file if there is a matchCompare CSV file with while read loop in bash
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have two csv file as below, i want to compare file1 with file2 and if records present in file2 that complete row should remove from file1. field to compare here is ID and in original file its at 11th position.
FILE1.CSV
"NAME","CITY","MARKS","ID","C","NAME1","TYPE"
"A","XY","100","12","","31420","TYPE1"
"A","XY","100","13","","31420","TYPE1"
FILE2.CSV
"ID"
"11"
"12"
"25"
EXPECTED OUTPUT
"NAME","CITY","MARKS","ID","C","NAME1","TYPE"
"A","XY","100","13","","31420","TYPE1"
text-processing awk csv
add a comment |
I have two csv file as below, i want to compare file1 with file2 and if records present in file2 that complete row should remove from file1. field to compare here is ID and in original file its at 11th position.
FILE1.CSV
"NAME","CITY","MARKS","ID","C","NAME1","TYPE"
"A","XY","100","12","","31420","TYPE1"
"A","XY","100","13","","31420","TYPE1"
FILE2.CSV
"ID"
"11"
"12"
"25"
EXPECTED OUTPUT
"NAME","CITY","MARKS","ID","C","NAME1","TYPE"
"A","XY","100","13","","31420","TYPE1"
text-processing awk csv
add a comment |
I have two csv file as below, i want to compare file1 with file2 and if records present in file2 that complete row should remove from file1. field to compare here is ID and in original file its at 11th position.
FILE1.CSV
"NAME","CITY","MARKS","ID","C","NAME1","TYPE"
"A","XY","100","12","","31420","TYPE1"
"A","XY","100","13","","31420","TYPE1"
FILE2.CSV
"ID"
"11"
"12"
"25"
EXPECTED OUTPUT
"NAME","CITY","MARKS","ID","C","NAME1","TYPE"
"A","XY","100","13","","31420","TYPE1"
text-processing awk csv
I have two csv file as below, i want to compare file1 with file2 and if records present in file2 that complete row should remove from file1. field to compare here is ID and in original file its at 11th position.
FILE1.CSV
"NAME","CITY","MARKS","ID","C","NAME1","TYPE"
"A","XY","100","12","","31420","TYPE1"
"A","XY","100","13","","31420","TYPE1"
FILE2.CSV
"ID"
"11"
"12"
"25"
EXPECTED OUTPUT
"NAME","CITY","MARKS","ID","C","NAME1","TYPE"
"A","XY","100","13","","31420","TYPE1"
text-processing awk csv
text-processing awk csv
edited Apr 10 at 23:09
Jeff Schaller♦
45k1164147
45k1164147
asked Apr 10 at 22:11
user828007user828007
63
63
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Using the utilities from csvkit (package python3-csvkit
on my Ubuntu system):
$ csvsql --query '
SELECT * FROM FILE1 WHERE ID NOT IN (SELECT ID FROM FILE2)
' FILE1.CSV FILE2.CSV | csvformat -U1
"NAME","CITY","MARKS","ID","C","NAME1","TYPE"
"A","XY","100","13","","31420","TYPE1"
add a comment |
grep -v -wf file2.csv file1.csv
will print each line of file1.csv
which does not contain any word of file1.csv
.
Of course the header line is missing now. If you need it do this:
$ head -n1 file1.csv;grep -v -wf file2.csv file1.csv
If the first line of file2.csv
defines the column where the words shouldn't match, here's a awk
solution:
$ awk -v FS="," '
NR==FNR && NR==1 column=$1; next; # save the column name to which one will compare
NR==FNR data[$1]++; next; # save the list of words to match again
NR!=FNR && FNR==1 print; for(i=1;i<=NF;i++) if($i==column) c=i; next; # print header line of file1, find column number to which one will compare
if ($c in data == 0) print # for any following line check if the word is not in our list
' file2.csv file1.csv
My understanding is that the header offile1.csv
shows which column offile2.csv
you should inspect. It's not all columns.
– Sparhawk
Apr 11 at 4:44
You might be right, thanks! I've edited my answer.
– finswimmer
Apr 11 at 5:33
add a comment |
With Miller (https://github.com/johnkerl/miller/releases/tag/5.4.0) is
mlr --csv join --np --ul -j ID -f input_01.csv input_02.csv
Some notes:
--np
to not emit paired records--ul
to emit unpaired records from the left file
The left file is input_01.csv
"NAME","CITY","MARKS","ID","C","NAME1","TYPE"
"A","XY","100","12","","31420","TYPE1"
"A","XY","100","13","","31420","TYPE1"
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f511773%2fhow-to-compare-two-csv-files-and-display-unique-records%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using the utilities from csvkit (package python3-csvkit
on my Ubuntu system):
$ csvsql --query '
SELECT * FROM FILE1 WHERE ID NOT IN (SELECT ID FROM FILE2)
' FILE1.CSV FILE2.CSV | csvformat -U1
"NAME","CITY","MARKS","ID","C","NAME1","TYPE"
"A","XY","100","13","","31420","TYPE1"
add a comment |
Using the utilities from csvkit (package python3-csvkit
on my Ubuntu system):
$ csvsql --query '
SELECT * FROM FILE1 WHERE ID NOT IN (SELECT ID FROM FILE2)
' FILE1.CSV FILE2.CSV | csvformat -U1
"NAME","CITY","MARKS","ID","C","NAME1","TYPE"
"A","XY","100","13","","31420","TYPE1"
add a comment |
Using the utilities from csvkit (package python3-csvkit
on my Ubuntu system):
$ csvsql --query '
SELECT * FROM FILE1 WHERE ID NOT IN (SELECT ID FROM FILE2)
' FILE1.CSV FILE2.CSV | csvformat -U1
"NAME","CITY","MARKS","ID","C","NAME1","TYPE"
"A","XY","100","13","","31420","TYPE1"
Using the utilities from csvkit (package python3-csvkit
on my Ubuntu system):
$ csvsql --query '
SELECT * FROM FILE1 WHERE ID NOT IN (SELECT ID FROM FILE2)
' FILE1.CSV FILE2.CSV | csvformat -U1
"NAME","CITY","MARKS","ID","C","NAME1","TYPE"
"A","XY","100","13","","31420","TYPE1"
answered Apr 11 at 0:45
steeldriversteeldriver
37.8k45489
37.8k45489
add a comment |
add a comment |
grep -v -wf file2.csv file1.csv
will print each line of file1.csv
which does not contain any word of file1.csv
.
Of course the header line is missing now. If you need it do this:
$ head -n1 file1.csv;grep -v -wf file2.csv file1.csv
If the first line of file2.csv
defines the column where the words shouldn't match, here's a awk
solution:
$ awk -v FS="," '
NR==FNR && NR==1 column=$1; next; # save the column name to which one will compare
NR==FNR data[$1]++; next; # save the list of words to match again
NR!=FNR && FNR==1 print; for(i=1;i<=NF;i++) if($i==column) c=i; next; # print header line of file1, find column number to which one will compare
if ($c in data == 0) print # for any following line check if the word is not in our list
' file2.csv file1.csv
My understanding is that the header offile1.csv
shows which column offile2.csv
you should inspect. It's not all columns.
– Sparhawk
Apr 11 at 4:44
You might be right, thanks! I've edited my answer.
– finswimmer
Apr 11 at 5:33
add a comment |
grep -v -wf file2.csv file1.csv
will print each line of file1.csv
which does not contain any word of file1.csv
.
Of course the header line is missing now. If you need it do this:
$ head -n1 file1.csv;grep -v -wf file2.csv file1.csv
If the first line of file2.csv
defines the column where the words shouldn't match, here's a awk
solution:
$ awk -v FS="," '
NR==FNR && NR==1 column=$1; next; # save the column name to which one will compare
NR==FNR data[$1]++; next; # save the list of words to match again
NR!=FNR && FNR==1 print; for(i=1;i<=NF;i++) if($i==column) c=i; next; # print header line of file1, find column number to which one will compare
if ($c in data == 0) print # for any following line check if the word is not in our list
' file2.csv file1.csv
My understanding is that the header offile1.csv
shows which column offile2.csv
you should inspect. It's not all columns.
– Sparhawk
Apr 11 at 4:44
You might be right, thanks! I've edited my answer.
– finswimmer
Apr 11 at 5:33
add a comment |
grep -v -wf file2.csv file1.csv
will print each line of file1.csv
which does not contain any word of file1.csv
.
Of course the header line is missing now. If you need it do this:
$ head -n1 file1.csv;grep -v -wf file2.csv file1.csv
If the first line of file2.csv
defines the column where the words shouldn't match, here's a awk
solution:
$ awk -v FS="," '
NR==FNR && NR==1 column=$1; next; # save the column name to which one will compare
NR==FNR data[$1]++; next; # save the list of words to match again
NR!=FNR && FNR==1 print; for(i=1;i<=NF;i++) if($i==column) c=i; next; # print header line of file1, find column number to which one will compare
if ($c in data == 0) print # for any following line check if the word is not in our list
' file2.csv file1.csv
grep -v -wf file2.csv file1.csv
will print each line of file1.csv
which does not contain any word of file1.csv
.
Of course the header line is missing now. If you need it do this:
$ head -n1 file1.csv;grep -v -wf file2.csv file1.csv
If the first line of file2.csv
defines the column where the words shouldn't match, here's a awk
solution:
$ awk -v FS="," '
NR==FNR && NR==1 column=$1; next; # save the column name to which one will compare
NR==FNR data[$1]++; next; # save the list of words to match again
NR!=FNR && FNR==1 print; for(i=1;i<=NF;i++) if($i==column) c=i; next; # print header line of file1, find column number to which one will compare
if ($c in data == 0) print # for any following line check if the word is not in our list
' file2.csv file1.csv
edited Apr 11 at 5:32
answered Apr 11 at 4:27
finswimmerfinswimmer
72918
72918
My understanding is that the header offile1.csv
shows which column offile2.csv
you should inspect. It's not all columns.
– Sparhawk
Apr 11 at 4:44
You might be right, thanks! I've edited my answer.
– finswimmer
Apr 11 at 5:33
add a comment |
My understanding is that the header offile1.csv
shows which column offile2.csv
you should inspect. It's not all columns.
– Sparhawk
Apr 11 at 4:44
You might be right, thanks! I've edited my answer.
– finswimmer
Apr 11 at 5:33
My understanding is that the header of
file1.csv
shows which column of file2.csv
you should inspect. It's not all columns.– Sparhawk
Apr 11 at 4:44
My understanding is that the header of
file1.csv
shows which column of file2.csv
you should inspect. It's not all columns.– Sparhawk
Apr 11 at 4:44
You might be right, thanks! I've edited my answer.
– finswimmer
Apr 11 at 5:33
You might be right, thanks! I've edited my answer.
– finswimmer
Apr 11 at 5:33
add a comment |
With Miller (https://github.com/johnkerl/miller/releases/tag/5.4.0) is
mlr --csv join --np --ul -j ID -f input_01.csv input_02.csv
Some notes:
--np
to not emit paired records--ul
to emit unpaired records from the left file
The left file is input_01.csv
"NAME","CITY","MARKS","ID","C","NAME1","TYPE"
"A","XY","100","12","","31420","TYPE1"
"A","XY","100","13","","31420","TYPE1"
add a comment |
With Miller (https://github.com/johnkerl/miller/releases/tag/5.4.0) is
mlr --csv join --np --ul -j ID -f input_01.csv input_02.csv
Some notes:
--np
to not emit paired records--ul
to emit unpaired records from the left file
The left file is input_01.csv
"NAME","CITY","MARKS","ID","C","NAME1","TYPE"
"A","XY","100","12","","31420","TYPE1"
"A","XY","100","13","","31420","TYPE1"
add a comment |
With Miller (https://github.com/johnkerl/miller/releases/tag/5.4.0) is
mlr --csv join --np --ul -j ID -f input_01.csv input_02.csv
Some notes:
--np
to not emit paired records--ul
to emit unpaired records from the left file
The left file is input_01.csv
"NAME","CITY","MARKS","ID","C","NAME1","TYPE"
"A","XY","100","12","","31420","TYPE1"
"A","XY","100","13","","31420","TYPE1"
With Miller (https://github.com/johnkerl/miller/releases/tag/5.4.0) is
mlr --csv join --np --ul -j ID -f input_01.csv input_02.csv
Some notes:
--np
to not emit paired records--ul
to emit unpaired records from the left file
The left file is input_01.csv
"NAME","CITY","MARKS","ID","C","NAME1","TYPE"
"A","XY","100","12","","31420","TYPE1"
"A","XY","100","13","","31420","TYPE1"
answered Apr 11 at 7:36
aborrusoaborruso
373311
373311
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f511773%2fhow-to-compare-two-csv-files-and-display-unique-records%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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