Compare two files line by line and show only difference from file 2
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I want to compare two files line by line without sorting and show only difference from file2.
file 1.txt
:
one
two
three
four
five
six
seven
eight
nine
ten
file 2.txt
:
one
five
three
four
five
twelve
seven
eight
hundred
ten
Then output should be
five
twelve
hundred
I do not want to sort files.
command-line text-processing
New contributor
Vishal Patel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
2
down vote
favorite
I want to compare two files line by line without sorting and show only difference from file2.
file 1.txt
:
one
two
three
four
five
six
seven
eight
nine
ten
file 2.txt
:
one
five
three
four
five
twelve
seven
eight
hundred
ten
Then output should be
five
twelve
hundred
I do not want to sort files.
command-line text-processing
New contributor
Vishal Patel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Readman comm
– waltinator
2 hours ago
comm
will complain that order is not sorted.
– Kristopher Ives
2 hours ago
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I want to compare two files line by line without sorting and show only difference from file2.
file 1.txt
:
one
two
three
four
five
six
seven
eight
nine
ten
file 2.txt
:
one
five
three
four
five
twelve
seven
eight
hundred
ten
Then output should be
five
twelve
hundred
I do not want to sort files.
command-line text-processing
New contributor
Vishal Patel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I want to compare two files line by line without sorting and show only difference from file2.
file 1.txt
:
one
two
three
four
five
six
seven
eight
nine
ten
file 2.txt
:
one
five
three
four
five
twelve
seven
eight
hundred
ten
Then output should be
five
twelve
hundred
I do not want to sort files.
command-line text-processing
command-line text-processing
New contributor
Vishal Patel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Vishal Patel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 1 hour ago
muru
130k19275470
130k19275470
New contributor
Vishal Patel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 3 hours ago


Vishal Patel
111
111
New contributor
Vishal Patel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Vishal Patel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Vishal Patel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Readman comm
– waltinator
2 hours ago
comm
will complain that order is not sorted.
– Kristopher Ives
2 hours ago
add a comment |Â
1
Readman comm
– waltinator
2 hours ago
comm
will complain that order is not sorted.
– Kristopher Ives
2 hours ago
1
1
Read
man comm
– waltinator
2 hours ago
Read
man comm
– waltinator
2 hours ago
comm
will complain that order is not sorted.– Kristopher Ives
2 hours ago
comm
will complain that order is not sorted.– Kristopher Ives
2 hours ago
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
1
down vote
Doing line-by-line comparison using awk
, you could do:
awk ' getline x<"file2" $0!=x print x' file1
getline x<"file2"
reads the entire line from file2 and holds into x variable.print x
when line from file1 differ with line in file2.
Or same but shorter:
awk ' getline x<"file1" $0!=x' file2
add a comment |Â
up vote
0
down vote
You cannot do this without sorting your data. Even if you never explicitly run the sort
command, any solution will involving indexing or sorting the data and taking O(n) time or memory to do it. For example, a solution which goes through the files and keeps track of which lines are seen or not seen will take O(n) memory and a solution which sorts the files first will take O(n) time.
add a comment |Â
up vote
0
down vote
You can as well use diff
for that task:
diff --old-line-format="" --unchanged-line-format="" 1.txt 2.txt
Gives the following output:
five
twelve
hundred
1
You can omitgrep
and do it withdiff file1 file2 | awk '/>/print $2'
, also you could do it all withdiff --old-line-format="" --unchanged-line-format="" file1 file2
, so it will not print those lines. see--LTYPE-line-format=LFMT
section ofman diff
for details.
– Î±Ò“sýιη
41 mins ago
I sat here a while fiddling with diff and wasn't able to make it work with diff alone, will definitively change ma answer accordingly, Thank you
– Videonauth
31 mins ago
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Doing line-by-line comparison using awk
, you could do:
awk ' getline x<"file2" $0!=x print x' file1
getline x<"file2"
reads the entire line from file2 and holds into x variable.print x
when line from file1 differ with line in file2.
Or same but shorter:
awk ' getline x<"file1" $0!=x' file2
add a comment |Â
up vote
1
down vote
Doing line-by-line comparison using awk
, you could do:
awk ' getline x<"file2" $0!=x print x' file1
getline x<"file2"
reads the entire line from file2 and holds into x variable.print x
when line from file1 differ with line in file2.
Or same but shorter:
awk ' getline x<"file1" $0!=x' file2
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Doing line-by-line comparison using awk
, you could do:
awk ' getline x<"file2" $0!=x print x' file1
getline x<"file2"
reads the entire line from file2 and holds into x variable.print x
when line from file1 differ with line in file2.
Or same but shorter:
awk ' getline x<"file1" $0!=x' file2
Doing line-by-line comparison using awk
, you could do:
awk ' getline x<"file2" $0!=x print x' file1
getline x<"file2"
reads the entire line from file2 and holds into x variable.print x
when line from file1 differ with line in file2.
Or same but shorter:
awk ' getline x<"file1" $0!=x' file2
answered 1 hour ago
αғsýιη
23.5k2193153
23.5k2193153
add a comment |Â
add a comment |Â
up vote
0
down vote
You cannot do this without sorting your data. Even if you never explicitly run the sort
command, any solution will involving indexing or sorting the data and taking O(n) time or memory to do it. For example, a solution which goes through the files and keeps track of which lines are seen or not seen will take O(n) memory and a solution which sorts the files first will take O(n) time.
add a comment |Â
up vote
0
down vote
You cannot do this without sorting your data. Even if you never explicitly run the sort
command, any solution will involving indexing or sorting the data and taking O(n) time or memory to do it. For example, a solution which goes through the files and keeps track of which lines are seen or not seen will take O(n) memory and a solution which sorts the files first will take O(n) time.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You cannot do this without sorting your data. Even if you never explicitly run the sort
command, any solution will involving indexing or sorting the data and taking O(n) time or memory to do it. For example, a solution which goes through the files and keeps track of which lines are seen or not seen will take O(n) memory and a solution which sorts the files first will take O(n) time.
You cannot do this without sorting your data. Even if you never explicitly run the sort
command, any solution will involving indexing or sorting the data and taking O(n) time or memory to do it. For example, a solution which goes through the files and keeps track of which lines are seen or not seen will take O(n) memory and a solution which sorts the files first will take O(n) time.
answered 2 hours ago
Kristopher Ives
1,371613
1,371613
add a comment |Â
add a comment |Â
up vote
0
down vote
You can as well use diff
for that task:
diff --old-line-format="" --unchanged-line-format="" 1.txt 2.txt
Gives the following output:
five
twelve
hundred
1
You can omitgrep
and do it withdiff file1 file2 | awk '/>/print $2'
, also you could do it all withdiff --old-line-format="" --unchanged-line-format="" file1 file2
, so it will not print those lines. see--LTYPE-line-format=LFMT
section ofman diff
for details.
– Î±Ò“sýιη
41 mins ago
I sat here a while fiddling with diff and wasn't able to make it work with diff alone, will definitively change ma answer accordingly, Thank you
– Videonauth
31 mins ago
add a comment |Â
up vote
0
down vote
You can as well use diff
for that task:
diff --old-line-format="" --unchanged-line-format="" 1.txt 2.txt
Gives the following output:
five
twelve
hundred
1
You can omitgrep
and do it withdiff file1 file2 | awk '/>/print $2'
, also you could do it all withdiff --old-line-format="" --unchanged-line-format="" file1 file2
, so it will not print those lines. see--LTYPE-line-format=LFMT
section ofman diff
for details.
– Î±Ò“sýιη
41 mins ago
I sat here a while fiddling with diff and wasn't able to make it work with diff alone, will definitively change ma answer accordingly, Thank you
– Videonauth
31 mins ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can as well use diff
for that task:
diff --old-line-format="" --unchanged-line-format="" 1.txt 2.txt
Gives the following output:
five
twelve
hundred
You can as well use diff
for that task:
diff --old-line-format="" --unchanged-line-format="" 1.txt 2.txt
Gives the following output:
five
twelve
hundred
edited 33 mins ago
answered 1 hour ago


Videonauth
22.5k116897
22.5k116897
1
You can omitgrep
and do it withdiff file1 file2 | awk '/>/print $2'
, also you could do it all withdiff --old-line-format="" --unchanged-line-format="" file1 file2
, so it will not print those lines. see--LTYPE-line-format=LFMT
section ofman diff
for details.
– Î±Ò“sýιη
41 mins ago
I sat here a while fiddling with diff and wasn't able to make it work with diff alone, will definitively change ma answer accordingly, Thank you
– Videonauth
31 mins ago
add a comment |Â
1
You can omitgrep
and do it withdiff file1 file2 | awk '/>/print $2'
, also you could do it all withdiff --old-line-format="" --unchanged-line-format="" file1 file2
, so it will not print those lines. see--LTYPE-line-format=LFMT
section ofman diff
for details.
– Î±Ò“sýιη
41 mins ago
I sat here a while fiddling with diff and wasn't able to make it work with diff alone, will definitively change ma answer accordingly, Thank you
– Videonauth
31 mins ago
1
1
You can omit
grep
and do it with diff file1 file2 | awk '/>/print $2'
, also you could do it all with diff --old-line-format="" --unchanged-line-format="" file1 file2
, so it will not print those lines. see --LTYPE-line-format=LFMT
section of man diff
for details.– Î±Ò“sýιη
41 mins ago
You can omit
grep
and do it with diff file1 file2 | awk '/>/print $2'
, also you could do it all with diff --old-line-format="" --unchanged-line-format="" file1 file2
, so it will not print those lines. see --LTYPE-line-format=LFMT
section of man diff
for details.– Î±Ò“sýιη
41 mins ago
I sat here a while fiddling with diff and wasn't able to make it work with diff alone, will definitively change ma answer accordingly, Thank you
– Videonauth
31 mins ago
I sat here a while fiddling with diff and wasn't able to make it work with diff alone, will definitively change ma answer accordingly, Thank you
– Videonauth
31 mins ago
add a comment |Â
Vishal Patel is a new contributor. Be nice, and check out our Code of Conduct.
Vishal Patel is a new contributor. Be nice, and check out our Code of Conduct.
Vishal Patel is a new contributor. Be nice, and check out our Code of Conduct.
Vishal Patel is a new contributor. Be nice, and check out our Code of Conduct.
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%2faskubuntu.com%2fquestions%2f1079399%2fcompare-two-files-line-by-line-and-show-only-difference-from-file-2%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
1
Read
man comm
– waltinator
2 hours ago
comm
will complain that order is not sorted.– Kristopher Ives
2 hours ago