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;








1















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"









share|improve this question






























    1















    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"









    share|improve this question


























      1












      1








      1








      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"









      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 10 at 23:09









      Jeff Schaller

      45k1164147




      45k1164147










      asked Apr 10 at 22:11









      user828007user828007

      63




      63




















          3 Answers
          3






          active

          oldest

          votes


















          1














          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"





          share|improve this answer






























            0














            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 awksolution:



            $ 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





            share|improve this answer

























            • 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


















            0














            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"





            share|improve this answer























              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
              );



              );













              draft saved

              draft discarded


















              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









              1














              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"





              share|improve this answer



























                1














                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"





                share|improve this answer

























                  1












                  1








                  1







                  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"





                  share|improve this answer













                  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"






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Apr 11 at 0:45









                  steeldriversteeldriver

                  37.8k45489




                  37.8k45489























                      0














                      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 awksolution:



                      $ 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





                      share|improve this answer

























                      • 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















                      0














                      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 awksolution:



                      $ 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





                      share|improve this answer

























                      • 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













                      0












                      0








                      0







                      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 awksolution:



                      $ 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





                      share|improve this answer















                      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 awksolution:



                      $ 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






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Apr 11 at 5:32

























                      answered Apr 11 at 4:27









                      finswimmerfinswimmer

                      72918




                      72918












                      • 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

















                      • 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
















                      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











                      0














                      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"





                      share|improve this answer



























                        0














                        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"





                        share|improve this answer

























                          0












                          0








                          0







                          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"





                          share|improve this answer













                          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"






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Apr 11 at 7:36









                          aborrusoaborruso

                          373311




                          373311



























                              draft saved

                              draft discarded
















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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

                              getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

                              Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In 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 ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

                              Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.