Comparing two fields from two files and printing the common items from the files with the count from each...












-3














I have two files in a below-mentioned way



File1



Emp1 Dec10 12Am 
Emp2 Dec10 2Am
Emp3 Dec10 4Am
Emp2 Dec10 6Am
Emp1 Dec10 8Am


File2



Emp1 Dec11 12Pm 
Emp2 Dec11 2Am
Emp1 Dec11 3Am
Emp3 Dec11 4Am
Emp1 Dec11 5Am


So I am trying to get the output as



Emp1 presence on Dec10 was 2 times and on Dec11 was 3 times. 
Emp2 presence on Dec10 was 2 times and on Dec11 was 1 time.
Emp3 presence on Dec10 was 1 time and on Dec11 was 1 time.









share|improve this question





























    -3














    I have two files in a below-mentioned way



    File1



    Emp1 Dec10 12Am 
    Emp2 Dec10 2Am
    Emp3 Dec10 4Am
    Emp2 Dec10 6Am
    Emp1 Dec10 8Am


    File2



    Emp1 Dec11 12Pm 
    Emp2 Dec11 2Am
    Emp1 Dec11 3Am
    Emp3 Dec11 4Am
    Emp1 Dec11 5Am


    So I am trying to get the output as



    Emp1 presence on Dec10 was 2 times and on Dec11 was 3 times. 
    Emp2 presence on Dec10 was 2 times and on Dec11 was 1 time.
    Emp3 presence on Dec10 was 1 time and on Dec11 was 1 time.









    share|improve this question



























      -3












      -3








      -3







      I have two files in a below-mentioned way



      File1



      Emp1 Dec10 12Am 
      Emp2 Dec10 2Am
      Emp3 Dec10 4Am
      Emp2 Dec10 6Am
      Emp1 Dec10 8Am


      File2



      Emp1 Dec11 12Pm 
      Emp2 Dec11 2Am
      Emp1 Dec11 3Am
      Emp3 Dec11 4Am
      Emp1 Dec11 5Am


      So I am trying to get the output as



      Emp1 presence on Dec10 was 2 times and on Dec11 was 3 times. 
      Emp2 presence on Dec10 was 2 times and on Dec11 was 1 time.
      Emp3 presence on Dec10 was 1 time and on Dec11 was 1 time.









      share|improve this question















      I have two files in a below-mentioned way



      File1



      Emp1 Dec10 12Am 
      Emp2 Dec10 2Am
      Emp3 Dec10 4Am
      Emp2 Dec10 6Am
      Emp1 Dec10 8Am


      File2



      Emp1 Dec11 12Pm 
      Emp2 Dec11 2Am
      Emp1 Dec11 3Am
      Emp3 Dec11 4Am
      Emp1 Dec11 5Am


      So I am trying to get the output as



      Emp1 presence on Dec10 was 2 times and on Dec11 was 3 times. 
      Emp2 presence on Dec10 was 2 times and on Dec11 was 1 time.
      Emp3 presence on Dec10 was 1 time and on Dec11 was 1 time.






      shell-script shell awk sed python






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 11 at 12:52









      msp9011

      3,65543863




      3,65543863










      asked Dec 11 at 10:57









      CMN

      33




      33






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Try this,



          EMP=(`awk '!seen[$1]++ {print $1}' File1 File2`)

          for emp in "${EMP[@]}"
          do
          DAYS=`awk -v b=$emp '$1==b {print $2}' File1 File2 | sort | uniq -c | awk '{print $2" was "$1" times"}' | sed ':a;N;$!ba;s/n/ and on /g'`
          echo "$emp presence on $DAYS "
          done





          share|improve this answer





















          • Thanks, It works like a gem. Can you please brief a bit on the regex that you have used in that with sed and awk ?
            – CMN
            Dec 12 at 13:16













          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',
          autoActivateHeartbeat: false,
          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%2f487314%2fcomparing-two-fields-from-two-files-and-printing-the-common-items-from-the-files%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









          0














          Try this,



          EMP=(`awk '!seen[$1]++ {print $1}' File1 File2`)

          for emp in "${EMP[@]}"
          do
          DAYS=`awk -v b=$emp '$1==b {print $2}' File1 File2 | sort | uniq -c | awk '{print $2" was "$1" times"}' | sed ':a;N;$!ba;s/n/ and on /g'`
          echo "$emp presence on $DAYS "
          done





          share|improve this answer





















          • Thanks, It works like a gem. Can you please brief a bit on the regex that you have used in that with sed and awk ?
            – CMN
            Dec 12 at 13:16


















          0














          Try this,



          EMP=(`awk '!seen[$1]++ {print $1}' File1 File2`)

          for emp in "${EMP[@]}"
          do
          DAYS=`awk -v b=$emp '$1==b {print $2}' File1 File2 | sort | uniq -c | awk '{print $2" was "$1" times"}' | sed ':a;N;$!ba;s/n/ and on /g'`
          echo "$emp presence on $DAYS "
          done





          share|improve this answer





















          • Thanks, It works like a gem. Can you please brief a bit on the regex that you have used in that with sed and awk ?
            – CMN
            Dec 12 at 13:16
















          0












          0








          0






          Try this,



          EMP=(`awk '!seen[$1]++ {print $1}' File1 File2`)

          for emp in "${EMP[@]}"
          do
          DAYS=`awk -v b=$emp '$1==b {print $2}' File1 File2 | sort | uniq -c | awk '{print $2" was "$1" times"}' | sed ':a;N;$!ba;s/n/ and on /g'`
          echo "$emp presence on $DAYS "
          done





          share|improve this answer












          Try this,



          EMP=(`awk '!seen[$1]++ {print $1}' File1 File2`)

          for emp in "${EMP[@]}"
          do
          DAYS=`awk -v b=$emp '$1==b {print $2}' File1 File2 | sort | uniq -c | awk '{print $2" was "$1" times"}' | sed ':a;N;$!ba;s/n/ and on /g'`
          echo "$emp presence on $DAYS "
          done






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 11 at 13:13









          msp9011

          3,65543863




          3,65543863












          • Thanks, It works like a gem. Can you please brief a bit on the regex that you have used in that with sed and awk ?
            – CMN
            Dec 12 at 13:16




















          • Thanks, It works like a gem. Can you please brief a bit on the regex that you have used in that with sed and awk ?
            – CMN
            Dec 12 at 13:16


















          Thanks, It works like a gem. Can you please brief a bit on the regex that you have used in that with sed and awk ?
          – CMN
          Dec 12 at 13:16






          Thanks, It works like a gem. Can you please brief a bit on the regex that you have used in that with sed and awk ?
          – CMN
          Dec 12 at 13:16




















          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%2f487314%2fcomparing-two-fields-from-two-files-and-printing-the-common-items-from-the-files%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

          Morgemoulin

          Scott Moir

          Souastre