How to write the difference between two files into a file

Clash 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?
diff vimdiff
add a comment |Â
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?
diff vimdiff
possible duplicate unix.stackexchange.com/questions/114877/â¦
â Wissam Roujoulah
Mar 6 at 6:51
@WissamRoujoulah You see, I don't need to check ifa.txtis a subset ofb.txt. I've already known thata.txtis a subset ofb.txt. My question is how to print the difference into a file.
â Yves
Mar 6 at 6:56
add a comment |Â
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?
diff vimdiff
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?
diff vimdiff
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 ifa.txtis a subset ofb.txt. I've already known thata.txtis a subset ofb.txt. My question is how to print the difference into a file.
â Yves
Mar 6 at 6:56
add a comment |Â
possible duplicate unix.stackexchange.com/questions/114877/â¦
â Wissam Roujoulah
Mar 6 at 6:51
@WissamRoujoulah You see, I don't need to check ifa.txtis a subset ofb.txt. I've already known thata.txtis a subset ofb.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
add a comment |Â
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
+1.commlooks like a very handy command during file comparison. Butdiff -uis giving a bunch of other information as well.
â Utsav
Mar 6 at 7:18
1
The extra information fromdiffis context and instructions (e.g.-to delete a line,+to add a line) that allowb.txtto be reconstructed froma.txtandc.txt- i.e.c.txtis a patch file, understood by thepatchprogram. Also simple enough for people to easily understand. BTW,diffhas several different output styles,-uis 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.colordiffis also handy for colourising diff output.
â cas
Mar 6 at 7:41
add a comment |Â
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
add a comment |Â
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
+1.commlooks like a very handy command during file comparison. Butdiff -uis giving a bunch of other information as well.
â Utsav
Mar 6 at 7:18
1
The extra information fromdiffis context and instructions (e.g.-to delete a line,+to add a line) that allowb.txtto be reconstructed froma.txtandc.txt- i.e.c.txtis a patch file, understood by thepatchprogram. Also simple enough for people to easily understand. BTW,diffhas several different output styles,-uis 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.colordiffis also handy for colourising diff output.
â cas
Mar 6 at 7:41
add a comment |Â
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
+1.commlooks like a very handy command during file comparison. Butdiff -uis giving a bunch of other information as well.
â Utsav
Mar 6 at 7:18
1
The extra information fromdiffis context and instructions (e.g.-to delete a line,+to add a line) that allowb.txtto be reconstructed froma.txtandc.txt- i.e.c.txtis a patch file, understood by thepatchprogram. Also simple enough for people to easily understand. BTW,diffhas several different output styles,-uis 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.colordiffis also handy for colourising diff output.
â cas
Mar 6 at 7:41
add a comment |Â
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
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
answered Mar 6 at 6:57
cas
37.6k44392
37.6k44392
+1.commlooks like a very handy command during file comparison. Butdiff -uis giving a bunch of other information as well.
â Utsav
Mar 6 at 7:18
1
The extra information fromdiffis context and instructions (e.g.-to delete a line,+to add a line) that allowb.txtto be reconstructed froma.txtandc.txt- i.e.c.txtis a patch file, understood by thepatchprogram. Also simple enough for people to easily understand. BTW,diffhas several different output styles,-uis 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.colordiffis also handy for colourising diff output.
â cas
Mar 6 at 7:41
add a comment |Â
+1.commlooks like a very handy command during file comparison. Butdiff -uis giving a bunch of other information as well.
â Utsav
Mar 6 at 7:18
1
The extra information fromdiffis context and instructions (e.g.-to delete a line,+to add a line) that allowb.txtto be reconstructed froma.txtandc.txt- i.e.c.txtis a patch file, understood by thepatchprogram. Also simple enough for people to easily understand. BTW,diffhas several different output styles,-uis 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.colordiffis 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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
edited Mar 6 at 7:11
answered Mar 6 at 7:04
Utsav
34519
34519
add a comment |Â
add a comment |Â
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
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
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
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
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
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.txtis a subset ofb.txt. I've already known thata.txtis a subset ofb.txt. My question is how to print the difference into a file.â Yves
Mar 6 at 6:56