How to compare two file to get expected result





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1















File A



Name|Dawson|Age|22|Stream|EEE
Name|Deepak|Stream|Mech
Name|Aruna|Age|20|Stream|Bio-tech


File B



Name|Age|Stream


Expected Output



Name|Age|Stream
Dawson|22|EEE
Deepak||Mech
Aruna|20|Bio-tech


I want to compare column 1 of file A against column 1 of file B, if matches then have to pull column 2 else should pull empty value.










share|improve this question




















  • 1





    Does fileB really only have one line? And should that line always be printed? And why do you say you only want column 2? Your output is showing columns 1, 2 and 3 from fileB and 2, 4 and 6 from fileA.

    – terdon
    Mar 29 at 11:38













  • I would try cat file2.txt; join -t"|" -e 'NULL' -o '1.2,1.4,1.6' file1.txt file2.txt ... But I do think there is another idea behind and maybe you'll should use a database (SQLite?) for your data.

    – LupusE
    Mar 29 at 11:47











  • @dessert : I need to compare and pull data and not just combine files. Because the file might be dynamic in future so i dont need to specify the columns. Rephrased the file A content.

    – Praveen P
    Mar 29 at 12:00


















1















File A



Name|Dawson|Age|22|Stream|EEE
Name|Deepak|Stream|Mech
Name|Aruna|Age|20|Stream|Bio-tech


File B



Name|Age|Stream


Expected Output



Name|Age|Stream
Dawson|22|EEE
Deepak||Mech
Aruna|20|Bio-tech


I want to compare column 1 of file A against column 1 of file B, if matches then have to pull column 2 else should pull empty value.










share|improve this question




















  • 1





    Does fileB really only have one line? And should that line always be printed? And why do you say you only want column 2? Your output is showing columns 1, 2 and 3 from fileB and 2, 4 and 6 from fileA.

    – terdon
    Mar 29 at 11:38













  • I would try cat file2.txt; join -t"|" -e 'NULL' -o '1.2,1.4,1.6' file1.txt file2.txt ... But I do think there is another idea behind and maybe you'll should use a database (SQLite?) for your data.

    – LupusE
    Mar 29 at 11:47











  • @dessert : I need to compare and pull data and not just combine files. Because the file might be dynamic in future so i dont need to specify the columns. Rephrased the file A content.

    – Praveen P
    Mar 29 at 12:00














1












1








1


1






File A



Name|Dawson|Age|22|Stream|EEE
Name|Deepak|Stream|Mech
Name|Aruna|Age|20|Stream|Bio-tech


File B



Name|Age|Stream


Expected Output



Name|Age|Stream
Dawson|22|EEE
Deepak||Mech
Aruna|20|Bio-tech


I want to compare column 1 of file A against column 1 of file B, if matches then have to pull column 2 else should pull empty value.










share|improve this question
















File A



Name|Dawson|Age|22|Stream|EEE
Name|Deepak|Stream|Mech
Name|Aruna|Age|20|Stream|Bio-tech


File B



Name|Age|Stream


Expected Output



Name|Age|Stream
Dawson|22|EEE
Deepak||Mech
Aruna|20|Bio-tech


I want to compare column 1 of file A against column 1 of file B, if matches then have to pull column 2 else should pull empty value.







command-line text-processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 29 at 11:59







Praveen P

















asked Mar 29 at 11:24









Praveen PPraveen P

62




62








  • 1





    Does fileB really only have one line? And should that line always be printed? And why do you say you only want column 2? Your output is showing columns 1, 2 and 3 from fileB and 2, 4 and 6 from fileA.

    – terdon
    Mar 29 at 11:38













  • I would try cat file2.txt; join -t"|" -e 'NULL' -o '1.2,1.4,1.6' file1.txt file2.txt ... But I do think there is another idea behind and maybe you'll should use a database (SQLite?) for your data.

    – LupusE
    Mar 29 at 11:47











  • @dessert : I need to compare and pull data and not just combine files. Because the file might be dynamic in future so i dont need to specify the columns. Rephrased the file A content.

    – Praveen P
    Mar 29 at 12:00














  • 1





    Does fileB really only have one line? And should that line always be printed? And why do you say you only want column 2? Your output is showing columns 1, 2 and 3 from fileB and 2, 4 and 6 from fileA.

    – terdon
    Mar 29 at 11:38













  • I would try cat file2.txt; join -t"|" -e 'NULL' -o '1.2,1.4,1.6' file1.txt file2.txt ... But I do think there is another idea behind and maybe you'll should use a database (SQLite?) for your data.

    – LupusE
    Mar 29 at 11:47











  • @dessert : I need to compare and pull data and not just combine files. Because the file might be dynamic in future so i dont need to specify the columns. Rephrased the file A content.

    – Praveen P
    Mar 29 at 12:00








1




1





Does fileB really only have one line? And should that line always be printed? And why do you say you only want column 2? Your output is showing columns 1, 2 and 3 from fileB and 2, 4 and 6 from fileA.

– terdon
Mar 29 at 11:38







Does fileB really only have one line? And should that line always be printed? And why do you say you only want column 2? Your output is showing columns 1, 2 and 3 from fileB and 2, 4 and 6 from fileA.

– terdon
Mar 29 at 11:38















I would try cat file2.txt; join -t"|" -e 'NULL' -o '1.2,1.4,1.6' file1.txt file2.txt ... But I do think there is another idea behind and maybe you'll should use a database (SQLite?) for your data.

– LupusE
Mar 29 at 11:47





I would try cat file2.txt; join -t"|" -e 'NULL' -o '1.2,1.4,1.6' file1.txt file2.txt ... But I do think there is another idea behind and maybe you'll should use a database (SQLite?) for your data.

– LupusE
Mar 29 at 11:47













@dessert : I need to compare and pull data and not just combine files. Because the file might be dynamic in future so i dont need to specify the columns. Rephrased the file A content.

– Praveen P
Mar 29 at 12:00





@dessert : I need to compare and pull data and not just combine files. Because the file might be dynamic in future so i dont need to specify the columns. Rephrased the file A content.

– Praveen P
Mar 29 at 12:00










1 Answer
1






active

oldest

votes


















1














If you map each pair of pipe-separated fields as key-value into a hash (or associative array), you can then do the lookup fairly easily e.g.



awk -F'[|]' '
NR==FNR {
print;
n = split($0,keys);
next
}
{
delete a;
for(i=1;i<NF;i+=2) a[$i]=$(i+1);
for(k=1;k<n;k++) printf "%s|", a[keys[k]];
print a[keys[n]]
}
' FileB FileA


The mapping is neater in perl



perl -F'[|]' -lpe '
BEGIN{@keys = qw(Name Age Stream); print join "|", @keys}
(%h) = @F; $_ = join "|", map { $h{$_} } @keys
' FileA


(obtaining the @keys from FileB is left as an exercise).






share|improve this answer
























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "89"
    };
    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2faskubuntu.com%2fquestions%2f1129660%2fhow-to-compare-two-file-to-get-expected-result%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    If you map each pair of pipe-separated fields as key-value into a hash (or associative array), you can then do the lookup fairly easily e.g.



    awk -F'[|]' '
    NR==FNR {
    print;
    n = split($0,keys);
    next
    }
    {
    delete a;
    for(i=1;i<NF;i+=2) a[$i]=$(i+1);
    for(k=1;k<n;k++) printf "%s|", a[keys[k]];
    print a[keys[n]]
    }
    ' FileB FileA


    The mapping is neater in perl



    perl -F'[|]' -lpe '
    BEGIN{@keys = qw(Name Age Stream); print join "|", @keys}
    (%h) = @F; $_ = join "|", map { $h{$_} } @keys
    ' FileA


    (obtaining the @keys from FileB is left as an exercise).






    share|improve this answer




























      1














      If you map each pair of pipe-separated fields as key-value into a hash (or associative array), you can then do the lookup fairly easily e.g.



      awk -F'[|]' '
      NR==FNR {
      print;
      n = split($0,keys);
      next
      }
      {
      delete a;
      for(i=1;i<NF;i+=2) a[$i]=$(i+1);
      for(k=1;k<n;k++) printf "%s|", a[keys[k]];
      print a[keys[n]]
      }
      ' FileB FileA


      The mapping is neater in perl



      perl -F'[|]' -lpe '
      BEGIN{@keys = qw(Name Age Stream); print join "|", @keys}
      (%h) = @F; $_ = join "|", map { $h{$_} } @keys
      ' FileA


      (obtaining the @keys from FileB is left as an exercise).






      share|improve this answer


























        1












        1








        1







        If you map each pair of pipe-separated fields as key-value into a hash (or associative array), you can then do the lookup fairly easily e.g.



        awk -F'[|]' '
        NR==FNR {
        print;
        n = split($0,keys);
        next
        }
        {
        delete a;
        for(i=1;i<NF;i+=2) a[$i]=$(i+1);
        for(k=1;k<n;k++) printf "%s|", a[keys[k]];
        print a[keys[n]]
        }
        ' FileB FileA


        The mapping is neater in perl



        perl -F'[|]' -lpe '
        BEGIN{@keys = qw(Name Age Stream); print join "|", @keys}
        (%h) = @F; $_ = join "|", map { $h{$_} } @keys
        ' FileA


        (obtaining the @keys from FileB is left as an exercise).






        share|improve this answer













        If you map each pair of pipe-separated fields as key-value into a hash (or associative array), you can then do the lookup fairly easily e.g.



        awk -F'[|]' '
        NR==FNR {
        print;
        n = split($0,keys);
        next
        }
        {
        delete a;
        for(i=1;i<NF;i+=2) a[$i]=$(i+1);
        for(k=1;k<n;k++) printf "%s|", a[keys[k]];
        print a[keys[n]]
        }
        ' FileB FileA


        The mapping is neater in perl



        perl -F'[|]' -lpe '
        BEGIN{@keys = qw(Name Age Stream); print join "|", @keys}
        (%h) = @F; $_ = join "|", map { $h{$_} } @keys
        ' FileA


        (obtaining the @keys from FileB is left as an exercise).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 29 at 15:33









        steeldriversteeldriver

        70.9k11115187




        70.9k11115187






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Ask Ubuntu!


            • 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%2faskubuntu.com%2fquestions%2f1129660%2fhow-to-compare-two-file-to-get-expected-result%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

            數位音樂下載

            格利澤436b

            When can things happen in Etherscan, such as the picture below?