Compare 2 files and store the output as file1_value,file2_value,Match/NoMatch
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have 2 files,
file1 ->
1
2
2
3
5
file2 ->
1
3
2
6
I want to the output to be stored in a 3rd file called file3 as
1,1,Match
2,2,Match
2,,NoMatch
3,3,Match
5,,NoMatch
,6,NoMatch
I've tried,
sort file1 > file1sorted.txt
sort file2 > file2sorted.txt
# Combine the sorted files with a comma and store it in a new file
paste -d ',' file1sorted.txt file2sorted.txt > mergedsortedfile.txt
# Compare the columns and store the result in a new file
awk -F',' 'print $1 == $2 ? "MATCH" : "NO MATCH"' mergedsortedfile.txt > result.txt
# Merge the result file with the already existing merged file
paste -d ', ' mergedsortedfile.txt result.txt > final_result.txt
The result appears like this,
1,1,MATCH
2,2,MATCH
2,3,NO MATCH
3,6,NO MATCH
5,,NO MATCH
shell-script text-processing
add a comment |
up vote
1
down vote
favorite
I have 2 files,
file1 ->
1
2
2
3
5
file2 ->
1
3
2
6
I want to the output to be stored in a 3rd file called file3 as
1,1,Match
2,2,Match
2,,NoMatch
3,3,Match
5,,NoMatch
,6,NoMatch
I've tried,
sort file1 > file1sorted.txt
sort file2 > file2sorted.txt
# Combine the sorted files with a comma and store it in a new file
paste -d ',' file1sorted.txt file2sorted.txt > mergedsortedfile.txt
# Compare the columns and store the result in a new file
awk -F',' 'print $1 == $2 ? "MATCH" : "NO MATCH"' mergedsortedfile.txt > result.txt
# Merge the result file with the already existing merged file
paste -d ', ' mergedsortedfile.txt result.txt > final_result.txt
The result appears like this,
1,1,MATCH
2,2,MATCH
2,3,NO MATCH
3,6,NO MATCH
5,,NO MATCH
shell-script text-processing
As the line number in both files can be different your approach won't work. I would pass both the files to awk and when the first file is read (can be checked with FILENAME variable) create an array. Then when the second file is read compare its lines with the array contents.
– ka3ak
Feb 6 '17 at 7:19
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have 2 files,
file1 ->
1
2
2
3
5
file2 ->
1
3
2
6
I want to the output to be stored in a 3rd file called file3 as
1,1,Match
2,2,Match
2,,NoMatch
3,3,Match
5,,NoMatch
,6,NoMatch
I've tried,
sort file1 > file1sorted.txt
sort file2 > file2sorted.txt
# Combine the sorted files with a comma and store it in a new file
paste -d ',' file1sorted.txt file2sorted.txt > mergedsortedfile.txt
# Compare the columns and store the result in a new file
awk -F',' 'print $1 == $2 ? "MATCH" : "NO MATCH"' mergedsortedfile.txt > result.txt
# Merge the result file with the already existing merged file
paste -d ', ' mergedsortedfile.txt result.txt > final_result.txt
The result appears like this,
1,1,MATCH
2,2,MATCH
2,3,NO MATCH
3,6,NO MATCH
5,,NO MATCH
shell-script text-processing
I have 2 files,
file1 ->
1
2
2
3
5
file2 ->
1
3
2
6
I want to the output to be stored in a 3rd file called file3 as
1,1,Match
2,2,Match
2,,NoMatch
3,3,Match
5,,NoMatch
,6,NoMatch
I've tried,
sort file1 > file1sorted.txt
sort file2 > file2sorted.txt
# Combine the sorted files with a comma and store it in a new file
paste -d ',' file1sorted.txt file2sorted.txt > mergedsortedfile.txt
# Compare the columns and store the result in a new file
awk -F',' 'print $1 == $2 ? "MATCH" : "NO MATCH"' mergedsortedfile.txt > result.txt
# Merge the result file with the already existing merged file
paste -d ', ' mergedsortedfile.txt result.txt > final_result.txt
The result appears like this,
1,1,MATCH
2,2,MATCH
2,3,NO MATCH
3,6,NO MATCH
5,,NO MATCH
shell-script text-processing
shell-script text-processing
edited Nov 25 at 15:11
Rui F Ribeiro
38.3k1476127
38.3k1476127
asked Feb 6 '17 at 5:18
akilesh raj
103
103
As the line number in both files can be different your approach won't work. I would pass both the files to awk and when the first file is read (can be checked with FILENAME variable) create an array. Then when the second file is read compare its lines with the array contents.
– ka3ak
Feb 6 '17 at 7:19
add a comment |
As the line number in both files can be different your approach won't work. I would pass both the files to awk and when the first file is read (can be checked with FILENAME variable) create an array. Then when the second file is read compare its lines with the array contents.
– ka3ak
Feb 6 '17 at 7:19
As the line number in both files can be different your approach won't work. I would pass both the files to awk and when the first file is read (can be checked with FILENAME variable) create an array. Then when the second file is read compare its lines with the array contents.
– ka3ak
Feb 6 '17 at 7:19
As the line number in both files can be different your approach won't work. I would pass both the files to awk and when the first file is read (can be checked with FILENAME variable) create an array. Then when the second file is read compare its lines with the array contents.
– ka3ak
Feb 6 '17 at 7:19
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Using comm
on the sorted data:
$ comm <( sort -n file1 ) <( sort -n file2 )
1
2
2
3
5
6
This output is tab-delimited. We can mark everything in columns 1 and 2 as "NoMatch" and in column 3 as "Match" with awk
:
$ comm <( sort -n file1 ) <( sort -n file2 ) |
awk -F$'t' 'BEGIN OFS="," $3 print $3, $3, "Match"; next print $1, $2, "NoMatch" '
1,1,Match
2,2,Match
2,,NoMatch
3,3,Match
5,,NoMatch
,6,NoMatch
The awk
script will read tab-delimited input (-F$'t'
) and use commas for the output field delimiter (OFS=","
). If there's something in field 3, then it will output it twice with Match
in the third field and continue with the next line. Otherwise, it will output fields 1 and 2 from the input together with NoMatch
in the third field.
Good! If this solves your issue, please consider accepting the answer.
– Kusalananda
Feb 6 '17 at 11:57
add a comment |
up vote
0
down vote
Save this perl script as file xxx an run it with perl xxx file1 file2
#!/usr/bin/perl
# save the first two files, the <> slurp clears @ARGV
($f1,$f2) = @ARGV;
# build a hash of hash of lines from all files,
# with the filename as key
do chomp; push @$hash$ARGV, $_ while <>;
# compare every line until both are empty
# the hash slice is a short expression for
# $a = $hash$f1->[$x]
# $b = $hash$f2->[$x]
for ($x=0;;$x++)
($a,$b) = map $$_[$x] @hash$f1,$f2;
last unless $a or $b;
printf "%s,%s,%sn", $a, $b, $a eq $b ? 'Match' : 'NoMatch';
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Using comm
on the sorted data:
$ comm <( sort -n file1 ) <( sort -n file2 )
1
2
2
3
5
6
This output is tab-delimited. We can mark everything in columns 1 and 2 as "NoMatch" and in column 3 as "Match" with awk
:
$ comm <( sort -n file1 ) <( sort -n file2 ) |
awk -F$'t' 'BEGIN OFS="," $3 print $3, $3, "Match"; next print $1, $2, "NoMatch" '
1,1,Match
2,2,Match
2,,NoMatch
3,3,Match
5,,NoMatch
,6,NoMatch
The awk
script will read tab-delimited input (-F$'t'
) and use commas for the output field delimiter (OFS=","
). If there's something in field 3, then it will output it twice with Match
in the third field and continue with the next line. Otherwise, it will output fields 1 and 2 from the input together with NoMatch
in the third field.
Good! If this solves your issue, please consider accepting the answer.
– Kusalananda
Feb 6 '17 at 11:57
add a comment |
up vote
2
down vote
accepted
Using comm
on the sorted data:
$ comm <( sort -n file1 ) <( sort -n file2 )
1
2
2
3
5
6
This output is tab-delimited. We can mark everything in columns 1 and 2 as "NoMatch" and in column 3 as "Match" with awk
:
$ comm <( sort -n file1 ) <( sort -n file2 ) |
awk -F$'t' 'BEGIN OFS="," $3 print $3, $3, "Match"; next print $1, $2, "NoMatch" '
1,1,Match
2,2,Match
2,,NoMatch
3,3,Match
5,,NoMatch
,6,NoMatch
The awk
script will read tab-delimited input (-F$'t'
) and use commas for the output field delimiter (OFS=","
). If there's something in field 3, then it will output it twice with Match
in the third field and continue with the next line. Otherwise, it will output fields 1 and 2 from the input together with NoMatch
in the third field.
Good! If this solves your issue, please consider accepting the answer.
– Kusalananda
Feb 6 '17 at 11:57
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Using comm
on the sorted data:
$ comm <( sort -n file1 ) <( sort -n file2 )
1
2
2
3
5
6
This output is tab-delimited. We can mark everything in columns 1 and 2 as "NoMatch" and in column 3 as "Match" with awk
:
$ comm <( sort -n file1 ) <( sort -n file2 ) |
awk -F$'t' 'BEGIN OFS="," $3 print $3, $3, "Match"; next print $1, $2, "NoMatch" '
1,1,Match
2,2,Match
2,,NoMatch
3,3,Match
5,,NoMatch
,6,NoMatch
The awk
script will read tab-delimited input (-F$'t'
) and use commas for the output field delimiter (OFS=","
). If there's something in field 3, then it will output it twice with Match
in the third field and continue with the next line. Otherwise, it will output fields 1 and 2 from the input together with NoMatch
in the third field.
Using comm
on the sorted data:
$ comm <( sort -n file1 ) <( sort -n file2 )
1
2
2
3
5
6
This output is tab-delimited. We can mark everything in columns 1 and 2 as "NoMatch" and in column 3 as "Match" with awk
:
$ comm <( sort -n file1 ) <( sort -n file2 ) |
awk -F$'t' 'BEGIN OFS="," $3 print $3, $3, "Match"; next print $1, $2, "NoMatch" '
1,1,Match
2,2,Match
2,,NoMatch
3,3,Match
5,,NoMatch
,6,NoMatch
The awk
script will read tab-delimited input (-F$'t'
) and use commas for the output field delimiter (OFS=","
). If there's something in field 3, then it will output it twice with Match
in the third field and continue with the next line. Otherwise, it will output fields 1 and 2 from the input together with NoMatch
in the third field.
edited Feb 6 '17 at 11:53
answered Feb 6 '17 at 10:35
Kusalananda
118k16223361
118k16223361
Good! If this solves your issue, please consider accepting the answer.
– Kusalananda
Feb 6 '17 at 11:57
add a comment |
Good! If this solves your issue, please consider accepting the answer.
– Kusalananda
Feb 6 '17 at 11:57
Good! If this solves your issue, please consider accepting the answer.
– Kusalananda
Feb 6 '17 at 11:57
Good! If this solves your issue, please consider accepting the answer.
– Kusalananda
Feb 6 '17 at 11:57
add a comment |
up vote
0
down vote
Save this perl script as file xxx an run it with perl xxx file1 file2
#!/usr/bin/perl
# save the first two files, the <> slurp clears @ARGV
($f1,$f2) = @ARGV;
# build a hash of hash of lines from all files,
# with the filename as key
do chomp; push @$hash$ARGV, $_ while <>;
# compare every line until both are empty
# the hash slice is a short expression for
# $a = $hash$f1->[$x]
# $b = $hash$f2->[$x]
for ($x=0;;$x++)
($a,$b) = map $$_[$x] @hash$f1,$f2;
last unless $a or $b;
printf "%s,%s,%sn", $a, $b, $a eq $b ? 'Match' : 'NoMatch';
add a comment |
up vote
0
down vote
Save this perl script as file xxx an run it with perl xxx file1 file2
#!/usr/bin/perl
# save the first two files, the <> slurp clears @ARGV
($f1,$f2) = @ARGV;
# build a hash of hash of lines from all files,
# with the filename as key
do chomp; push @$hash$ARGV, $_ while <>;
# compare every line until both are empty
# the hash slice is a short expression for
# $a = $hash$f1->[$x]
# $b = $hash$f2->[$x]
for ($x=0;;$x++)
($a,$b) = map $$_[$x] @hash$f1,$f2;
last unless $a or $b;
printf "%s,%s,%sn", $a, $b, $a eq $b ? 'Match' : 'NoMatch';
add a comment |
up vote
0
down vote
up vote
0
down vote
Save this perl script as file xxx an run it with perl xxx file1 file2
#!/usr/bin/perl
# save the first two files, the <> slurp clears @ARGV
($f1,$f2) = @ARGV;
# build a hash of hash of lines from all files,
# with the filename as key
do chomp; push @$hash$ARGV, $_ while <>;
# compare every line until both are empty
# the hash slice is a short expression for
# $a = $hash$f1->[$x]
# $b = $hash$f2->[$x]
for ($x=0;;$x++)
($a,$b) = map $$_[$x] @hash$f1,$f2;
last unless $a or $b;
printf "%s,%s,%sn", $a, $b, $a eq $b ? 'Match' : 'NoMatch';
Save this perl script as file xxx an run it with perl xxx file1 file2
#!/usr/bin/perl
# save the first two files, the <> slurp clears @ARGV
($f1,$f2) = @ARGV;
# build a hash of hash of lines from all files,
# with the filename as key
do chomp; push @$hash$ARGV, $_ while <>;
# compare every line until both are empty
# the hash slice is a short expression for
# $a = $hash$f1->[$x]
# $b = $hash$f2->[$x]
for ($x=0;;$x++)
($a,$b) = map $$_[$x] @hash$f1,$f2;
last unless $a or $b;
printf "%s,%s,%sn", $a, $b, $a eq $b ? 'Match' : 'NoMatch';
answered Feb 6 '17 at 9:57
ingopingo
61944
61944
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f342791%2fcompare-2-files-and-store-the-output-as-file1-value-file2-value-match-nomatch%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
As the line number in both files can be different your approach won't work. I would pass both the files to awk and when the first file is read (can be checked with FILENAME variable) create an array. Then when the second file is read compare its lines with the array contents.
– ka3ak
Feb 6 '17 at 7:19