Comparing Data between 2 different files in Unix

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have 2 different files-
File 1
2
4
6
8
10
12
File 2
2
3
5
6
10
12
I want to compare 2 files and get the output data which is in File 1 but not in File 2-
Output
4
8
I am using below command but not getting desired output-
comm -23 file1 file2
shell-script files comm
add a comment |Â
up vote
0
down vote
favorite
I have 2 different files-
File 1
2
4
6
8
10
12
File 2
2
3
5
6
10
12
I want to compare 2 files and get the output data which is in File 1 but not in File 2-
Output
4
8
I am using below command but not getting desired output-
comm -23 file1 file2
shell-script files comm
I think the problem iscommexpects a lexicographic sort which is different from the numeric order of your inputs. Lexicographically,1,10, and12all need to sort before2, so you need to resort your file1 and file2 to use comm.
â jw013
Jun 13 at 15:34
1
You'd be getting an error fromcomm. It's always important to share these in your question.
â roaima
Jun 13 at 19:07
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have 2 different files-
File 1
2
4
6
8
10
12
File 2
2
3
5
6
10
12
I want to compare 2 files and get the output data which is in File 1 but not in File 2-
Output
4
8
I am using below command but not getting desired output-
comm -23 file1 file2
shell-script files comm
I have 2 different files-
File 1
2
4
6
8
10
12
File 2
2
3
5
6
10
12
I want to compare 2 files and get the output data which is in File 1 but not in File 2-
Output
4
8
I am using below command but not getting desired output-
comm -23 file1 file2
shell-script files comm
asked Jun 13 at 15:26
Praveen Verma
2316
2316
I think the problem iscommexpects a lexicographic sort which is different from the numeric order of your inputs. Lexicographically,1,10, and12all need to sort before2, so you need to resort your file1 and file2 to use comm.
â jw013
Jun 13 at 15:34
1
You'd be getting an error fromcomm. It's always important to share these in your question.
â roaima
Jun 13 at 19:07
add a comment |Â
I think the problem iscommexpects a lexicographic sort which is different from the numeric order of your inputs. Lexicographically,1,10, and12all need to sort before2, so you need to resort your file1 and file2 to use comm.
â jw013
Jun 13 at 15:34
1
You'd be getting an error fromcomm. It's always important to share these in your question.
â roaima
Jun 13 at 19:07
I think the problem is
comm expects a lexicographic sort which is different from the numeric order of your inputs. Lexicographically, 1, 10, and 12 all need to sort before 2, so you need to resort your file1 and file2 to use comm.â jw013
Jun 13 at 15:34
I think the problem is
comm expects a lexicographic sort which is different from the numeric order of your inputs. Lexicographically, 1, 10, and 12 all need to sort before 2, so you need to resort your file1 and file2 to use comm.â jw013
Jun 13 at 15:34
1
1
You'd be getting an error from
comm. It's always important to share these in your question.â roaima
Jun 13 at 19:07
You'd be getting an error from
comm. It's always important to share these in your question.â roaima
Jun 13 at 19:07
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
For comm to work properly, both files have to be sorted lexicographically, not numerically. You may sort your files before calling comm using
sort -o file1 file1
sort -o file2 file2
Then:
$ comm -23 file1 file2
4
8
Or, you may sort the files at the same time as you call comm, if your shell supports process substitutions:
$ comm -23 <( sort file1 ) <( sort file2 )
4
8
add a comment |Â
up vote
2
down vote
You can use grep
grep -F -x -f 'File2' -v 'File1'
Pattern of fixed--strings (-F) in File2 (-f)
Use -x to match whole lines. Otherwise "0123" in File1 would be excluded from the output due to "12" in File2.
find similar line and inverse with (-v)
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
For comm to work properly, both files have to be sorted lexicographically, not numerically. You may sort your files before calling comm using
sort -o file1 file1
sort -o file2 file2
Then:
$ comm -23 file1 file2
4
8
Or, you may sort the files at the same time as you call comm, if your shell supports process substitutions:
$ comm -23 <( sort file1 ) <( sort file2 )
4
8
add a comment |Â
up vote
4
down vote
accepted
For comm to work properly, both files have to be sorted lexicographically, not numerically. You may sort your files before calling comm using
sort -o file1 file1
sort -o file2 file2
Then:
$ comm -23 file1 file2
4
8
Or, you may sort the files at the same time as you call comm, if your shell supports process substitutions:
$ comm -23 <( sort file1 ) <( sort file2 )
4
8
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
For comm to work properly, both files have to be sorted lexicographically, not numerically. You may sort your files before calling comm using
sort -o file1 file1
sort -o file2 file2
Then:
$ comm -23 file1 file2
4
8
Or, you may sort the files at the same time as you call comm, if your shell supports process substitutions:
$ comm -23 <( sort file1 ) <( sort file2 )
4
8
For comm to work properly, both files have to be sorted lexicographically, not numerically. You may sort your files before calling comm using
sort -o file1 file1
sort -o file2 file2
Then:
$ comm -23 file1 file2
4
8
Or, you may sort the files at the same time as you call comm, if your shell supports process substitutions:
$ comm -23 <( sort file1 ) <( sort file2 )
4
8
answered Jun 13 at 15:36
Kusalananda
101k13199312
101k13199312
add a comment |Â
add a comment |Â
up vote
2
down vote
You can use grep
grep -F -x -f 'File2' -v 'File1'
Pattern of fixed--strings (-F) in File2 (-f)
Use -x to match whole lines. Otherwise "0123" in File1 would be excluded from the output due to "12" in File2.
find similar line and inverse with (-v)
add a comment |Â
up vote
2
down vote
You can use grep
grep -F -x -f 'File2' -v 'File1'
Pattern of fixed--strings (-F) in File2 (-f)
Use -x to match whole lines. Otherwise "0123" in File1 would be excluded from the output due to "12" in File2.
find similar line and inverse with (-v)
add a comment |Â
up vote
2
down vote
up vote
2
down vote
You can use grep
grep -F -x -f 'File2' -v 'File1'
Pattern of fixed--strings (-F) in File2 (-f)
Use -x to match whole lines. Otherwise "0123" in File1 would be excluded from the output due to "12" in File2.
find similar line and inverse with (-v)
You can use grep
grep -F -x -f 'File2' -v 'File1'
Pattern of fixed--strings (-F) in File2 (-f)
Use -x to match whole lines. Otherwise "0123" in File1 would be excluded from the output due to "12" in File2.
find similar line and inverse with (-v)
edited Jun 13 at 15:53
glenn jackman
45.6k265100
45.6k265100
answered Jun 13 at 15:46
ctac_
996116
996116
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%2f449578%2fcomparing-data-between-2-different-files-in-unix%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
I think the problem is
commexpects a lexicographic sort which is different from the numeric order of your inputs. Lexicographically,1,10, and12all need to sort before2, so you need to resort your file1 and file2 to use comm.â jw013
Jun 13 at 15:34
1
You'd be getting an error from
comm. It's always important to share these in your question.â roaima
Jun 13 at 19:07