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
Do we know why communications with Beresheet and NASA were lost during the attempted landing of the Moon lander?
Antler Helmet: Can it work?
How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time
How can I make names more distinctive without making them longer?
Writing Thesis: Copying from published papers
How do you clear the ApexPages.getMessages() collection in a test?
Was credit for the black hole image misattributed?
I'm having difficulty getting my players to do stuff in a sandbox campaign
How should I respond to a player wanting to catch a sword between their hands?
Problem when applying foreach loop
Using "nakedly" instead of "with nothing on"
Is there a service that would inform me whenever a new direct route is scheduled from a given airport?
I'm thinking of a number
Two different pronunciation of "понял"
Are my PIs rude or am I just being too sensitive?
Why does this iterative way of solving of equation work?
Windows 10: How to Lock (not sleep) laptop on lid close?
What is the largest species of polychaete?
Mortgage adviser recommends a longer term than necessary combined with overpayments
Unexpected result with right shift after bitwise negation
Cold is to Refrigerator as warm is to?
Estimated State payment too big --> money back; + 2018 Tax Reform
Classification of bundles, Postnikov towers, obstruction theory, local coefficients
Why use gamma over alpha radiation?
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