Bash Script for showing difference between two text files

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











up vote
4
down vote

favorite












Lets say I have two lists:



guests-2016.txt:



Peter
Michael
Frank
Dirk


guests-2017.txt:



Mark
Michael
Dirk
Lilly


How may I create two new lists of guests



  1. Guests that were in guests-2016.txt but are not in guests-2017.txt (former_guests.txt)


  2. Guests that were not in guests-2016.txt but are in guests-2017.txt now (new_guests.txt)


Blank lines should be ignored. Only standard utilities should be used.



My idea would be to use diff and do some post processing.










share|improve this question



















  • 6




    Check out the manual page for comm. Then add what you have tried, and explain how it did not work as you intended.
    – DopeGhoti
    Sep 26 '17 at 19:09














up vote
4
down vote

favorite












Lets say I have two lists:



guests-2016.txt:



Peter
Michael
Frank
Dirk


guests-2017.txt:



Mark
Michael
Dirk
Lilly


How may I create two new lists of guests



  1. Guests that were in guests-2016.txt but are not in guests-2017.txt (former_guests.txt)


  2. Guests that were not in guests-2016.txt but are in guests-2017.txt now (new_guests.txt)


Blank lines should be ignored. Only standard utilities should be used.



My idea would be to use diff and do some post processing.










share|improve this question



















  • 6




    Check out the manual page for comm. Then add what you have tried, and explain how it did not work as you intended.
    – DopeGhoti
    Sep 26 '17 at 19:09












up vote
4
down vote

favorite









up vote
4
down vote

favorite











Lets say I have two lists:



guests-2016.txt:



Peter
Michael
Frank
Dirk


guests-2017.txt:



Mark
Michael
Dirk
Lilly


How may I create two new lists of guests



  1. Guests that were in guests-2016.txt but are not in guests-2017.txt (former_guests.txt)


  2. Guests that were not in guests-2016.txt but are in guests-2017.txt now (new_guests.txt)


Blank lines should be ignored. Only standard utilities should be used.



My idea would be to use diff and do some post processing.










share|improve this question















Lets say I have two lists:



guests-2016.txt:



Peter
Michael
Frank
Dirk


guests-2017.txt:



Mark
Michael
Dirk
Lilly


How may I create two new lists of guests



  1. Guests that were in guests-2016.txt but are not in guests-2017.txt (former_guests.txt)


  2. Guests that were not in guests-2016.txt but are in guests-2017.txt now (new_guests.txt)


Blank lines should be ignored. Only standard utilities should be used.



My idea would be to use diff and do some post processing.







shell-script text-processing scripting diff






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 26 '17 at 20:23









Jeff Schaller

32.3k849110




32.3k849110










asked Sep 26 '17 at 19:07









NoobieNoob

232




232







  • 6




    Check out the manual page for comm. Then add what you have tried, and explain how it did not work as you intended.
    – DopeGhoti
    Sep 26 '17 at 19:09












  • 6




    Check out the manual page for comm. Then add what you have tried, and explain how it did not work as you intended.
    – DopeGhoti
    Sep 26 '17 at 19:09







6




6




Check out the manual page for comm. Then add what you have tried, and explain how it did not work as you intended.
– DopeGhoti
Sep 26 '17 at 19:09




Check out the manual page for comm. Then add what you have tried, and explain how it did not work as you intended.
– DopeGhoti
Sep 26 '17 at 19:09










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










Given two sorted files, comm would do this for you.



See the combinations of the -2 -3 and -1 -3 command line options, for example.






share|improve this answer




















  • Thanks I did not know comm before. How to skip blank lines?
    – NoobieNoob
    Sep 26 '17 at 19:32










  • @NoobieNoob There are no blank lines in you example data, nor will there be any in the output from comm.
    – Kusalananda
    Sep 26 '17 at 19:41

















up vote
1
down vote













Check, does it do the job. I can add an explanation, if you are needed.



awk '
/^$/next
FNR == NR guest_2016[$1] = 1
FNR != NR
if(!guest_2016[$1])
print $1 > "new_guests.txt"
delete guest_2016[$1];

END
for(i in guest_2016)
print i > "former_guests.txt"
' guests-2016.txt guests-2017.txt





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',
    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%2f394612%2fbash-script-for-showing-difference-between-two-text-files%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    Given two sorted files, comm would do this for you.



    See the combinations of the -2 -3 and -1 -3 command line options, for example.






    share|improve this answer




















    • Thanks I did not know comm before. How to skip blank lines?
      – NoobieNoob
      Sep 26 '17 at 19:32










    • @NoobieNoob There are no blank lines in you example data, nor will there be any in the output from comm.
      – Kusalananda
      Sep 26 '17 at 19:41














    up vote
    3
    down vote



    accepted










    Given two sorted files, comm would do this for you.



    See the combinations of the -2 -3 and -1 -3 command line options, for example.






    share|improve this answer




















    • Thanks I did not know comm before. How to skip blank lines?
      – NoobieNoob
      Sep 26 '17 at 19:32










    • @NoobieNoob There are no blank lines in you example data, nor will there be any in the output from comm.
      – Kusalananda
      Sep 26 '17 at 19:41












    up vote
    3
    down vote



    accepted







    up vote
    3
    down vote



    accepted






    Given two sorted files, comm would do this for you.



    See the combinations of the -2 -3 and -1 -3 command line options, for example.






    share|improve this answer












    Given two sorted files, comm would do this for you.



    See the combinations of the -2 -3 and -1 -3 command line options, for example.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Sep 26 '17 at 19:25









    Kusalananda

    106k14209327




    106k14209327











    • Thanks I did not know comm before. How to skip blank lines?
      – NoobieNoob
      Sep 26 '17 at 19:32










    • @NoobieNoob There are no blank lines in you example data, nor will there be any in the output from comm.
      – Kusalananda
      Sep 26 '17 at 19:41
















    • Thanks I did not know comm before. How to skip blank lines?
      – NoobieNoob
      Sep 26 '17 at 19:32










    • @NoobieNoob There are no blank lines in you example data, nor will there be any in the output from comm.
      – Kusalananda
      Sep 26 '17 at 19:41















    Thanks I did not know comm before. How to skip blank lines?
    – NoobieNoob
    Sep 26 '17 at 19:32




    Thanks I did not know comm before. How to skip blank lines?
    – NoobieNoob
    Sep 26 '17 at 19:32












    @NoobieNoob There are no blank lines in you example data, nor will there be any in the output from comm.
    – Kusalananda
    Sep 26 '17 at 19:41




    @NoobieNoob There are no blank lines in you example data, nor will there be any in the output from comm.
    – Kusalananda
    Sep 26 '17 at 19:41












    up vote
    1
    down vote













    Check, does it do the job. I can add an explanation, if you are needed.



    awk '
    /^$/next
    FNR == NR guest_2016[$1] = 1
    FNR != NR
    if(!guest_2016[$1])
    print $1 > "new_guests.txt"
    delete guest_2016[$1];

    END
    for(i in guest_2016)
    print i > "former_guests.txt"
    ' guests-2016.txt guests-2017.txt





    share|improve this answer
























      up vote
      1
      down vote













      Check, does it do the job. I can add an explanation, if you are needed.



      awk '
      /^$/next
      FNR == NR guest_2016[$1] = 1
      FNR != NR
      if(!guest_2016[$1])
      print $1 > "new_guests.txt"
      delete guest_2016[$1];

      END
      for(i in guest_2016)
      print i > "former_guests.txt"
      ' guests-2016.txt guests-2017.txt





      share|improve this answer






















        up vote
        1
        down vote










        up vote
        1
        down vote









        Check, does it do the job. I can add an explanation, if you are needed.



        awk '
        /^$/next
        FNR == NR guest_2016[$1] = 1
        FNR != NR
        if(!guest_2016[$1])
        print $1 > "new_guests.txt"
        delete guest_2016[$1];

        END
        for(i in guest_2016)
        print i > "former_guests.txt"
        ' guests-2016.txt guests-2017.txt





        share|improve this answer












        Check, does it do the job. I can add an explanation, if you are needed.



        awk '
        /^$/next
        FNR == NR guest_2016[$1] = 1
        FNR != NR
        if(!guest_2016[$1])
        print $1 > "new_guests.txt"
        delete guest_2016[$1];

        END
        for(i in guest_2016)
        print i > "former_guests.txt"
        ' guests-2016.txt guests-2017.txt






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 26 '17 at 20:41









        MiniMax

        2,706719




        2,706719



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f394612%2fbash-script-for-showing-difference-between-two-text-files%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

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

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?