Compare 2 files and store the output as file1_value,file2_value,Match/NoMatch

The name of the pictureThe name of the pictureThe name of the pictureClash 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









share|improve this question























  • 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














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









share|improve this question























  • 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












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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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










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.








share|improve this answer






















  • Good! If this solves your issue, please consider accepting the answer.
    – Kusalananda
    Feb 6 '17 at 11:57

















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';






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: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    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%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

























    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.








    share|improve this answer






















    • Good! If this solves your issue, please consider accepting the answer.
      – Kusalananda
      Feb 6 '17 at 11:57














    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.








    share|improve this answer






















    • Good! If this solves your issue, please consider accepting the answer.
      – Kusalananda
      Feb 6 '17 at 11:57












    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.








    share|improve this answer














    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.









    share|improve this answer














    share|improve this answer



    share|improve this answer








    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
















    • 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












    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';






    share|improve this answer
























      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';






      share|improve this answer






















        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';






        share|improve this answer












        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';







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 6 '17 at 9:57









        ingopingo

        61944




        61944



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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






            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