How to write the difference between two files into a file

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











up vote
0
down vote

favorite












Saying that I have two files: a.txt and b.txt.



The content of a.txt:



hello world


The content of b.txt:



hello world
something else


Of course I can use vimdiff to check their difference, I can make sure that a.txt is a subset of b.txt, which means that b.txt must contain all of lines existing in a.txt (just like the example above).



My question is how to record lines which exists in b.txt but doesn't exist in a.txt into a file?







share|improve this question




















  • possible duplicate unix.stackexchange.com/questions/114877/…
    – Wissam Roujoulah
    Mar 6 at 6:51










  • @WissamRoujoulah You see, I don't need to check if a.txt is a subset of b.txt. I've already known that a.txt is a subset of b.txt. My question is how to print the difference into a file.
    – Yves
    Mar 6 at 6:56














up vote
0
down vote

favorite












Saying that I have two files: a.txt and b.txt.



The content of a.txt:



hello world


The content of b.txt:



hello world
something else


Of course I can use vimdiff to check their difference, I can make sure that a.txt is a subset of b.txt, which means that b.txt must contain all of lines existing in a.txt (just like the example above).



My question is how to record lines which exists in b.txt but doesn't exist in a.txt into a file?







share|improve this question




















  • possible duplicate unix.stackexchange.com/questions/114877/…
    – Wissam Roujoulah
    Mar 6 at 6:51










  • @WissamRoujoulah You see, I don't need to check if a.txt is a subset of b.txt. I've already known that a.txt is a subset of b.txt. My question is how to print the difference into a file.
    – Yves
    Mar 6 at 6:56












up vote
0
down vote

favorite









up vote
0
down vote

favorite











Saying that I have two files: a.txt and b.txt.



The content of a.txt:



hello world


The content of b.txt:



hello world
something else


Of course I can use vimdiff to check their difference, I can make sure that a.txt is a subset of b.txt, which means that b.txt must contain all of lines existing in a.txt (just like the example above).



My question is how to record lines which exists in b.txt but doesn't exist in a.txt into a file?







share|improve this question












Saying that I have two files: a.txt and b.txt.



The content of a.txt:



hello world


The content of b.txt:



hello world
something else


Of course I can use vimdiff to check their difference, I can make sure that a.txt is a subset of b.txt, which means that b.txt must contain all of lines existing in a.txt (just like the example above).



My question is how to record lines which exists in b.txt but doesn't exist in a.txt into a file?









share|improve this question











share|improve this question




share|improve this question










asked Mar 6 at 6:47









Yves

705414




705414











  • possible duplicate unix.stackexchange.com/questions/114877/…
    – Wissam Roujoulah
    Mar 6 at 6:51










  • @WissamRoujoulah You see, I don't need to check if a.txt is a subset of b.txt. I've already known that a.txt is a subset of b.txt. My question is how to print the difference into a file.
    – Yves
    Mar 6 at 6:56
















  • possible duplicate unix.stackexchange.com/questions/114877/…
    – Wissam Roujoulah
    Mar 6 at 6:51










  • @WissamRoujoulah You see, I don't need to check if a.txt is a subset of b.txt. I've already known that a.txt is a subset of b.txt. My question is how to print the difference into a file.
    – Yves
    Mar 6 at 6:56















possible duplicate unix.stackexchange.com/questions/114877/…
– Wissam Roujoulah
Mar 6 at 6:51




possible duplicate unix.stackexchange.com/questions/114877/…
– Wissam Roujoulah
Mar 6 at 6:51












@WissamRoujoulah You see, I don't need to check if a.txt is a subset of b.txt. I've already known that a.txt is a subset of b.txt. My question is how to print the difference into a file.
– Yves
Mar 6 at 6:56




@WissamRoujoulah You see, I don't need to check if a.txt is a subset of b.txt. I've already known that a.txt is a subset of b.txt. My question is how to print the difference into a file.
– Yves
Mar 6 at 6:56










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










comm -1 -3 a.txt b.txt > c.txt


The -1 excludes lines that are only in a.txt, and the -3 excludes lines that are in both. Thus only the lines exclusively in b.txt are output (see man comm or comm --help for details). The output is redirected to c.txt



If you want the difference between the two files, use diff rather than comm. e.g.



diff -u a.txt b.txt > c.txt





share|improve this answer




















  • +1. comm looks like a very handy command during file comparison. But diff -u is giving a bunch of other information as well.
    – Utsav
    Mar 6 at 7:18






  • 1




    The extra information from diff is context and instructions (e.g. - to delete a line, + to add a line) that allow b.txt to be reconstructed from a.txt and c.txt - i.e. c.txt is a patch file, understood by the patch program. Also simple enough for people to easily understand. BTW, diff has several different output styles, -u is the "unified diff" format. There are also various specialised version of diff - e.g. to do side-by-side comparisons, or compare 3 files at once, or find word differences within a line. colordiff is also handy for colourising diff output.
    – cas
    Mar 6 at 7:41

















up vote
1
down vote













If you dont care for subset, you can use just



diff a.txt b.txt|grep ">"|cut -c 3- > foo.txt


.



$ cat a.txt
hello world
$ cat b.txt
hello world
something else
$ diff a.txt b.txt|grep ">"|cut -c 3- > foo.txt
$ cat foo.txt
something else





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%2f428419%2fhow-to-write-the-difference-between-two-files-into-a-file%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










    comm -1 -3 a.txt b.txt > c.txt


    The -1 excludes lines that are only in a.txt, and the -3 excludes lines that are in both. Thus only the lines exclusively in b.txt are output (see man comm or comm --help for details). The output is redirected to c.txt



    If you want the difference between the two files, use diff rather than comm. e.g.



    diff -u a.txt b.txt > c.txt





    share|improve this answer




















    • +1. comm looks like a very handy command during file comparison. But diff -u is giving a bunch of other information as well.
      – Utsav
      Mar 6 at 7:18






    • 1




      The extra information from diff is context and instructions (e.g. - to delete a line, + to add a line) that allow b.txt to be reconstructed from a.txt and c.txt - i.e. c.txt is a patch file, understood by the patch program. Also simple enough for people to easily understand. BTW, diff has several different output styles, -u is the "unified diff" format. There are also various specialised version of diff - e.g. to do side-by-side comparisons, or compare 3 files at once, or find word differences within a line. colordiff is also handy for colourising diff output.
      – cas
      Mar 6 at 7:41














    up vote
    3
    down vote



    accepted










    comm -1 -3 a.txt b.txt > c.txt


    The -1 excludes lines that are only in a.txt, and the -3 excludes lines that are in both. Thus only the lines exclusively in b.txt are output (see man comm or comm --help for details). The output is redirected to c.txt



    If you want the difference between the two files, use diff rather than comm. e.g.



    diff -u a.txt b.txt > c.txt





    share|improve this answer




















    • +1. comm looks like a very handy command during file comparison. But diff -u is giving a bunch of other information as well.
      – Utsav
      Mar 6 at 7:18






    • 1




      The extra information from diff is context and instructions (e.g. - to delete a line, + to add a line) that allow b.txt to be reconstructed from a.txt and c.txt - i.e. c.txt is a patch file, understood by the patch program. Also simple enough for people to easily understand. BTW, diff has several different output styles, -u is the "unified diff" format. There are also various specialised version of diff - e.g. to do side-by-side comparisons, or compare 3 files at once, or find word differences within a line. colordiff is also handy for colourising diff output.
      – cas
      Mar 6 at 7:41












    up vote
    3
    down vote



    accepted







    up vote
    3
    down vote



    accepted






    comm -1 -3 a.txt b.txt > c.txt


    The -1 excludes lines that are only in a.txt, and the -3 excludes lines that are in both. Thus only the lines exclusively in b.txt are output (see man comm or comm --help for details). The output is redirected to c.txt



    If you want the difference between the two files, use diff rather than comm. e.g.



    diff -u a.txt b.txt > c.txt





    share|improve this answer












    comm -1 -3 a.txt b.txt > c.txt


    The -1 excludes lines that are only in a.txt, and the -3 excludes lines that are in both. Thus only the lines exclusively in b.txt are output (see man comm or comm --help for details). The output is redirected to c.txt



    If you want the difference between the two files, use diff rather than comm. e.g.



    diff -u a.txt b.txt > c.txt






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 6 at 6:57









    cas

    37.6k44392




    37.6k44392











    • +1. comm looks like a very handy command during file comparison. But diff -u is giving a bunch of other information as well.
      – Utsav
      Mar 6 at 7:18






    • 1




      The extra information from diff is context and instructions (e.g. - to delete a line, + to add a line) that allow b.txt to be reconstructed from a.txt and c.txt - i.e. c.txt is a patch file, understood by the patch program. Also simple enough for people to easily understand. BTW, diff has several different output styles, -u is the "unified diff" format. There are also various specialised version of diff - e.g. to do side-by-side comparisons, or compare 3 files at once, or find word differences within a line. colordiff is also handy for colourising diff output.
      – cas
      Mar 6 at 7:41
















    • +1. comm looks like a very handy command during file comparison. But diff -u is giving a bunch of other information as well.
      – Utsav
      Mar 6 at 7:18






    • 1




      The extra information from diff is context and instructions (e.g. - to delete a line, + to add a line) that allow b.txt to be reconstructed from a.txt and c.txt - i.e. c.txt is a patch file, understood by the patch program. Also simple enough for people to easily understand. BTW, diff has several different output styles, -u is the "unified diff" format. There are also various specialised version of diff - e.g. to do side-by-side comparisons, or compare 3 files at once, or find word differences within a line. colordiff is also handy for colourising diff output.
      – cas
      Mar 6 at 7:41















    +1. comm looks like a very handy command during file comparison. But diff -u is giving a bunch of other information as well.
    – Utsav
    Mar 6 at 7:18




    +1. comm looks like a very handy command during file comparison. But diff -u is giving a bunch of other information as well.
    – Utsav
    Mar 6 at 7:18




    1




    1




    The extra information from diff is context and instructions (e.g. - to delete a line, + to add a line) that allow b.txt to be reconstructed from a.txt and c.txt - i.e. c.txt is a patch file, understood by the patch program. Also simple enough for people to easily understand. BTW, diff has several different output styles, -u is the "unified diff" format. There are also various specialised version of diff - e.g. to do side-by-side comparisons, or compare 3 files at once, or find word differences within a line. colordiff is also handy for colourising diff output.
    – cas
    Mar 6 at 7:41




    The extra information from diff is context and instructions (e.g. - to delete a line, + to add a line) that allow b.txt to be reconstructed from a.txt and c.txt - i.e. c.txt is a patch file, understood by the patch program. Also simple enough for people to easily understand. BTW, diff has several different output styles, -u is the "unified diff" format. There are also various specialised version of diff - e.g. to do side-by-side comparisons, or compare 3 files at once, or find word differences within a line. colordiff is also handy for colourising diff output.
    – cas
    Mar 6 at 7:41












    up vote
    1
    down vote













    If you dont care for subset, you can use just



    diff a.txt b.txt|grep ">"|cut -c 3- > foo.txt


    .



    $ cat a.txt
    hello world
    $ cat b.txt
    hello world
    something else
    $ diff a.txt b.txt|grep ">"|cut -c 3- > foo.txt
    $ cat foo.txt
    something else





    share|improve this answer


























      up vote
      1
      down vote













      If you dont care for subset, you can use just



      diff a.txt b.txt|grep ">"|cut -c 3- > foo.txt


      .



      $ cat a.txt
      hello world
      $ cat b.txt
      hello world
      something else
      $ diff a.txt b.txt|grep ">"|cut -c 3- > foo.txt
      $ cat foo.txt
      something else





      share|improve this answer
























        up vote
        1
        down vote










        up vote
        1
        down vote









        If you dont care for subset, you can use just



        diff a.txt b.txt|grep ">"|cut -c 3- > foo.txt


        .



        $ cat a.txt
        hello world
        $ cat b.txt
        hello world
        something else
        $ diff a.txt b.txt|grep ">"|cut -c 3- > foo.txt
        $ cat foo.txt
        something else





        share|improve this answer














        If you dont care for subset, you can use just



        diff a.txt b.txt|grep ">"|cut -c 3- > foo.txt


        .



        $ cat a.txt
        hello world
        $ cat b.txt
        hello world
        something else
        $ diff a.txt b.txt|grep ">"|cut -c 3- > foo.txt
        $ cat foo.txt
        something else






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 6 at 7:11

























        answered Mar 6 at 7:04









        Utsav

        34519




        34519






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f428419%2fhow-to-write-the-difference-between-two-files-into-a-file%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?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay