Find values in a file from another file












1















I have 2 files, in one of this I have some values and I need to find them in another file. Instead to grep for each single value, I would use the first file to do lookup in the second file



i.e.



File 1



ns1.cloudns.net. -17554 IN A 85.159.233.17
ns1.cloudns.net. -17554 IN AAAA 2a00:1768:1001:9::1
www.alweya.com. -335336 IN A 192.69.217.246


File 2



alweya.com


Outout - File 1 contains www.alweya.com










share|improve this question

























  • What are you wanting as output? Do you want to know where each value is the the other file? Do you just want to know if it exists? Do you want a count of the occurrences?

    – David King
    Nov 16 '15 at 20:26











  • What is the format of these values? A word per line? Values with spaces?

    – Kira
    Nov 16 '15 at 20:28






  • 3





    @Federi edit your question add more information, please. Use code formatting for your example files.

    – muru
    Nov 16 '15 at 20:30






  • 1





    So your files have the patterns as a comma separated list on a single line?

    – terdon
    Nov 16 '15 at 20:34






  • 1





    As previously pointed out, please edit your question instead of adding new information here.

    – Kira
    Nov 16 '15 at 20:37
















1















I have 2 files, in one of this I have some values and I need to find them in another file. Instead to grep for each single value, I would use the first file to do lookup in the second file



i.e.



File 1



ns1.cloudns.net. -17554 IN A 85.159.233.17
ns1.cloudns.net. -17554 IN AAAA 2a00:1768:1001:9::1
www.alweya.com. -335336 IN A 192.69.217.246


File 2



alweya.com


Outout - File 1 contains www.alweya.com










share|improve this question

























  • What are you wanting as output? Do you want to know where each value is the the other file? Do you just want to know if it exists? Do you want a count of the occurrences?

    – David King
    Nov 16 '15 at 20:26











  • What is the format of these values? A word per line? Values with spaces?

    – Kira
    Nov 16 '15 at 20:28






  • 3





    @Federi edit your question add more information, please. Use code formatting for your example files.

    – muru
    Nov 16 '15 at 20:30






  • 1





    So your files have the patterns as a comma separated list on a single line?

    – terdon
    Nov 16 '15 at 20:34






  • 1





    As previously pointed out, please edit your question instead of adding new information here.

    – Kira
    Nov 16 '15 at 20:37














1












1








1


0






I have 2 files, in one of this I have some values and I need to find them in another file. Instead to grep for each single value, I would use the first file to do lookup in the second file



i.e.



File 1



ns1.cloudns.net. -17554 IN A 85.159.233.17
ns1.cloudns.net. -17554 IN AAAA 2a00:1768:1001:9::1
www.alweya.com. -335336 IN A 192.69.217.246


File 2



alweya.com


Outout - File 1 contains www.alweya.com










share|improve this question
















I have 2 files, in one of this I have some values and I need to find them in another file. Instead to grep for each single value, I would use the first file to do lookup in the second file



i.e.



File 1



ns1.cloudns.net. -17554 IN A 85.159.233.17
ns1.cloudns.net. -17554 IN AAAA 2a00:1768:1001:9::1
www.alweya.com. -335336 IN A 192.69.217.246


File 2



alweya.com


Outout - File 1 contains www.alweya.com







grep diff






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 16 mins ago









Rui F Ribeiro

40.1k1479136




40.1k1479136










asked Nov 16 '15 at 20:22









FederiFederi

45321230




45321230













  • What are you wanting as output? Do you want to know where each value is the the other file? Do you just want to know if it exists? Do you want a count of the occurrences?

    – David King
    Nov 16 '15 at 20:26











  • What is the format of these values? A word per line? Values with spaces?

    – Kira
    Nov 16 '15 at 20:28






  • 3





    @Federi edit your question add more information, please. Use code formatting for your example files.

    – muru
    Nov 16 '15 at 20:30






  • 1





    So your files have the patterns as a comma separated list on a single line?

    – terdon
    Nov 16 '15 at 20:34






  • 1





    As previously pointed out, please edit your question instead of adding new information here.

    – Kira
    Nov 16 '15 at 20:37



















  • What are you wanting as output? Do you want to know where each value is the the other file? Do you just want to know if it exists? Do you want a count of the occurrences?

    – David King
    Nov 16 '15 at 20:26











  • What is the format of these values? A word per line? Values with spaces?

    – Kira
    Nov 16 '15 at 20:28






  • 3





    @Federi edit your question add more information, please. Use code formatting for your example files.

    – muru
    Nov 16 '15 at 20:30






  • 1





    So your files have the patterns as a comma separated list on a single line?

    – terdon
    Nov 16 '15 at 20:34






  • 1





    As previously pointed out, please edit your question instead of adding new information here.

    – Kira
    Nov 16 '15 at 20:37

















What are you wanting as output? Do you want to know where each value is the the other file? Do you just want to know if it exists? Do you want a count of the occurrences?

– David King
Nov 16 '15 at 20:26





What are you wanting as output? Do you want to know where each value is the the other file? Do you just want to know if it exists? Do you want a count of the occurrences?

– David King
Nov 16 '15 at 20:26













What is the format of these values? A word per line? Values with spaces?

– Kira
Nov 16 '15 at 20:28





What is the format of these values? A word per line? Values with spaces?

– Kira
Nov 16 '15 at 20:28




3




3





@Federi edit your question add more information, please. Use code formatting for your example files.

– muru
Nov 16 '15 at 20:30





@Federi edit your question add more information, please. Use code formatting for your example files.

– muru
Nov 16 '15 at 20:30




1




1





So your files have the patterns as a comma separated list on a single line?

– terdon
Nov 16 '15 at 20:34





So your files have the patterns as a comma separated list on a single line?

– terdon
Nov 16 '15 at 20:34




1




1





As previously pointed out, please edit your question instead of adding new information here.

– Kira
Nov 16 '15 at 20:37





As previously pointed out, please edit your question instead of adding new information here.

– Kira
Nov 16 '15 at 20:37










2 Answers
2






active

oldest

votes


















1














while read line
do
msg="File 1 contains "
msg2="$msg$(grep "$line" file1.txt | cut -d ' ' -f 1)"
if [ ${#msg2} -ne ${#msg} ]; then
echo $msg2
fi
done < file2.txt


File 1



ns1.cloudns.net. -17554 IN A 85.159.233.17
ns1.cloudns.net. -17554 IN AAAA 2a00:1768:1001:9::1
www.alweya.com. -335336 IN A 192.69.217.246


File 2



alweya.com





share|improve this answer

































    1














    It depends on what exactly you want to do. The simplest approach is to use grep, passing a file of patterns with -f:



    $ grep -f file2 file1 
    www.alweya.com. -335336 IN A 192.69.217.246


    If you need to control the output in more detail, you can use a shell loop:



    $ while read pat; do 
    grep -q "$pat" file1 && echo "file1 contains $pat";
    done < file2
    file1 contains alweya.com


    The second approach lets you print individual messages for each pattern found but will be much, much slower on larger files.






    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',
      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%2f243409%2ffind-values-in-a-file-from-another-file%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









      1














      while read line
      do
      msg="File 1 contains "
      msg2="$msg$(grep "$line" file1.txt | cut -d ' ' -f 1)"
      if [ ${#msg2} -ne ${#msg} ]; then
      echo $msg2
      fi
      done < file2.txt


      File 1



      ns1.cloudns.net. -17554 IN A 85.159.233.17
      ns1.cloudns.net. -17554 IN AAAA 2a00:1768:1001:9::1
      www.alweya.com. -335336 IN A 192.69.217.246


      File 2



      alweya.com





      share|improve this answer






























        1














        while read line
        do
        msg="File 1 contains "
        msg2="$msg$(grep "$line" file1.txt | cut -d ' ' -f 1)"
        if [ ${#msg2} -ne ${#msg} ]; then
        echo $msg2
        fi
        done < file2.txt


        File 1



        ns1.cloudns.net. -17554 IN A 85.159.233.17
        ns1.cloudns.net. -17554 IN AAAA 2a00:1768:1001:9::1
        www.alweya.com. -335336 IN A 192.69.217.246


        File 2



        alweya.com





        share|improve this answer




























          1












          1








          1







          while read line
          do
          msg="File 1 contains "
          msg2="$msg$(grep "$line" file1.txt | cut -d ' ' -f 1)"
          if [ ${#msg2} -ne ${#msg} ]; then
          echo $msg2
          fi
          done < file2.txt


          File 1



          ns1.cloudns.net. -17554 IN A 85.159.233.17
          ns1.cloudns.net. -17554 IN AAAA 2a00:1768:1001:9::1
          www.alweya.com. -335336 IN A 192.69.217.246


          File 2



          alweya.com





          share|improve this answer















          while read line
          do
          msg="File 1 contains "
          msg2="$msg$(grep "$line" file1.txt | cut -d ' ' -f 1)"
          if [ ${#msg2} -ne ${#msg} ]; then
          echo $msg2
          fi
          done < file2.txt


          File 1



          ns1.cloudns.net. -17554 IN A 85.159.233.17
          ns1.cloudns.net. -17554 IN AAAA 2a00:1768:1001:9::1
          www.alweya.com. -335336 IN A 192.69.217.246


          File 2



          alweya.com






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 16 '15 at 23:04









          terdon

          130k32255433




          130k32255433










          answered Nov 16 '15 at 21:20









          ppflrsppflrs

          113




          113

























              1














              It depends on what exactly you want to do. The simplest approach is to use grep, passing a file of patterns with -f:



              $ grep -f file2 file1 
              www.alweya.com. -335336 IN A 192.69.217.246


              If you need to control the output in more detail, you can use a shell loop:



              $ while read pat; do 
              grep -q "$pat" file1 && echo "file1 contains $pat";
              done < file2
              file1 contains alweya.com


              The second approach lets you print individual messages for each pattern found but will be much, much slower on larger files.






              share|improve this answer






























                1














                It depends on what exactly you want to do. The simplest approach is to use grep, passing a file of patterns with -f:



                $ grep -f file2 file1 
                www.alweya.com. -335336 IN A 192.69.217.246


                If you need to control the output in more detail, you can use a shell loop:



                $ while read pat; do 
                grep -q "$pat" file1 && echo "file1 contains $pat";
                done < file2
                file1 contains alweya.com


                The second approach lets you print individual messages for each pattern found but will be much, much slower on larger files.






                share|improve this answer




























                  1












                  1








                  1







                  It depends on what exactly you want to do. The simplest approach is to use grep, passing a file of patterns with -f:



                  $ grep -f file2 file1 
                  www.alweya.com. -335336 IN A 192.69.217.246


                  If you need to control the output in more detail, you can use a shell loop:



                  $ while read pat; do 
                  grep -q "$pat" file1 && echo "file1 contains $pat";
                  done < file2
                  file1 contains alweya.com


                  The second approach lets you print individual messages for each pattern found but will be much, much slower on larger files.






                  share|improve this answer















                  It depends on what exactly you want to do. The simplest approach is to use grep, passing a file of patterns with -f:



                  $ grep -f file2 file1 
                  www.alweya.com. -335336 IN A 192.69.217.246


                  If you need to control the output in more detail, you can use a shell loop:



                  $ while read pat; do 
                  grep -q "$pat" file1 && echo "file1 contains $pat";
                  done < file2
                  file1 contains alweya.com


                  The second approach lets you print individual messages for each pattern found but will be much, much slower on larger files.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 17 '15 at 0:07

























                  answered Nov 16 '15 at 23:10









                  terdonterdon

                  130k32255433




                  130k32255433






























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f243409%2ffind-values-in-a-file-from-another-file%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

                      Accessing regular linux commands in Huawei's Dopra Linux

                      Can't connect RFCOMM socket: Host is down

                      Kernel panic - not syncing: Fatal Exception in Interrupt