How do I delete lines of 1st file if it ONLY EXACT Matche of the string present in the 2nd file in linux?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Consider I have two text files.



First File name - "Emails.txt" with the following data:



sale@gmail.com
info@yahoo.com
all@gmail.com
help@domain.com
abcdsale@gmail.com
abcd.info@yahoo.com


Second text file - "Roles.txt" with the following strings:



sale@
info@
all@
help@


How to delete all the lines in the 1st text file "Emails.txt" if it matches ONLY EXACT stings of any line present in the second text file "Role.txt"?



The desired output of the new file should be:



abcdsale@gmail.com
abcd.info@yahoo.com


I tried using



grep -vf Role.txt Emails.txt


But, this command also deletes the line if it matches all the characters of the Role.txt.



I want to remove only it matches EXACT as per Role.txt file, and swip everything if there are other characters before the desired string.



Here someone gave a similar solution for removing line matching EXACT string at the end of each lines, but in this I need the regrex to so the same at the beginning of each lines.










share|improve this question





























    0















    Consider I have two text files.



    First File name - "Emails.txt" with the following data:



    sale@gmail.com
    info@yahoo.com
    all@gmail.com
    help@domain.com
    abcdsale@gmail.com
    abcd.info@yahoo.com


    Second text file - "Roles.txt" with the following strings:



    sale@
    info@
    all@
    help@


    How to delete all the lines in the 1st text file "Emails.txt" if it matches ONLY EXACT stings of any line present in the second text file "Role.txt"?



    The desired output of the new file should be:



    abcdsale@gmail.com
    abcd.info@yahoo.com


    I tried using



    grep -vf Role.txt Emails.txt


    But, this command also deletes the line if it matches all the characters of the Role.txt.



    I want to remove only it matches EXACT as per Role.txt file, and swip everything if there are other characters before the desired string.



    Here someone gave a similar solution for removing line matching EXACT string at the end of each lines, but in this I need the regrex to so the same at the beginning of each lines.










    share|improve this question

























      0












      0








      0








      Consider I have two text files.



      First File name - "Emails.txt" with the following data:



      sale@gmail.com
      info@yahoo.com
      all@gmail.com
      help@domain.com
      abcdsale@gmail.com
      abcd.info@yahoo.com


      Second text file - "Roles.txt" with the following strings:



      sale@
      info@
      all@
      help@


      How to delete all the lines in the 1st text file "Emails.txt" if it matches ONLY EXACT stings of any line present in the second text file "Role.txt"?



      The desired output of the new file should be:



      abcdsale@gmail.com
      abcd.info@yahoo.com


      I tried using



      grep -vf Role.txt Emails.txt


      But, this command also deletes the line if it matches all the characters of the Role.txt.



      I want to remove only it matches EXACT as per Role.txt file, and swip everything if there are other characters before the desired string.



      Here someone gave a similar solution for removing line matching EXACT string at the end of each lines, but in this I need the regrex to so the same at the beginning of each lines.










      share|improve this question














      Consider I have two text files.



      First File name - "Emails.txt" with the following data:



      sale@gmail.com
      info@yahoo.com
      all@gmail.com
      help@domain.com
      abcdsale@gmail.com
      abcd.info@yahoo.com


      Second text file - "Roles.txt" with the following strings:



      sale@
      info@
      all@
      help@


      How to delete all the lines in the 1st text file "Emails.txt" if it matches ONLY EXACT stings of any line present in the second text file "Role.txt"?



      The desired output of the new file should be:



      abcdsale@gmail.com
      abcd.info@yahoo.com


      I tried using



      grep -vf Role.txt Emails.txt


      But, this command also deletes the line if it matches all the characters of the Role.txt.



      I want to remove only it matches EXACT as per Role.txt file, and swip everything if there are other characters before the desired string.



      Here someone gave a similar solution for removing line matching EXACT string at the end of each lines, but in this I need the regrex to so the same at the beginning of each lines.







      linux command-line grep sed






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 28 at 10:11









      Joney WalkerJoney Walker

      235




      235






















          1 Answer
          1






          active

          oldest

          votes


















          2














          The second file should look like this:



          ^sale@
          ^info@
          ^all@
          ^help@


          because ^ matches the beginning of a line.



          You can rebuild the file with sed -i 's/^/^/' Role.txt. To be clear: this will change the file. Then your original command



          grep -vf Role.txt Emails.txt


          will work.





          Alternatively, with your original Role.txt, in a shell that supports process substitution (e.g. Bash):



          grep -vf <(sed 's/^/^/' Role.txt) Emails.txt


          In this case sed adds ^ characters on the fly and Role.txt remains unchanged.






          share|improve this answer
























          • Perfect! This works great. :)

            – Joney Walker
            Jan 28 at 10:57











          • So if I want to do the same at the end of file, will this work if i add ^ at the end of line too? eg: @domail.co^ such that @domain.co.uk will be skipped? or should we substitute the end with $?

            – Joney Walker
            Jan 28 at 11:32













          • @JoneyWalker No, you probably want $. Please research regular expressions.

            – Kamil Maciorowski
            Jan 28 at 11:38











          • Ok got it... This works: grep -vf <(sed 's/$/$/' Test2.txt) Text1.txt

            – Joney Walker
            Jan 28 at 11:49












          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "3"
          };
          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%2fsuperuser.com%2fquestions%2f1399185%2fhow-do-i-delete-lines-of-1st-file-if-it-only-exact-matche-of-the-string-present%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









          2














          The second file should look like this:



          ^sale@
          ^info@
          ^all@
          ^help@


          because ^ matches the beginning of a line.



          You can rebuild the file with sed -i 's/^/^/' Role.txt. To be clear: this will change the file. Then your original command



          grep -vf Role.txt Emails.txt


          will work.





          Alternatively, with your original Role.txt, in a shell that supports process substitution (e.g. Bash):



          grep -vf <(sed 's/^/^/' Role.txt) Emails.txt


          In this case sed adds ^ characters on the fly and Role.txt remains unchanged.






          share|improve this answer
























          • Perfect! This works great. :)

            – Joney Walker
            Jan 28 at 10:57











          • So if I want to do the same at the end of file, will this work if i add ^ at the end of line too? eg: @domail.co^ such that @domain.co.uk will be skipped? or should we substitute the end with $?

            – Joney Walker
            Jan 28 at 11:32













          • @JoneyWalker No, you probably want $. Please research regular expressions.

            – Kamil Maciorowski
            Jan 28 at 11:38











          • Ok got it... This works: grep -vf <(sed 's/$/$/' Test2.txt) Text1.txt

            – Joney Walker
            Jan 28 at 11:49
















          2














          The second file should look like this:



          ^sale@
          ^info@
          ^all@
          ^help@


          because ^ matches the beginning of a line.



          You can rebuild the file with sed -i 's/^/^/' Role.txt. To be clear: this will change the file. Then your original command



          grep -vf Role.txt Emails.txt


          will work.





          Alternatively, with your original Role.txt, in a shell that supports process substitution (e.g. Bash):



          grep -vf <(sed 's/^/^/' Role.txt) Emails.txt


          In this case sed adds ^ characters on the fly and Role.txt remains unchanged.






          share|improve this answer
























          • Perfect! This works great. :)

            – Joney Walker
            Jan 28 at 10:57











          • So if I want to do the same at the end of file, will this work if i add ^ at the end of line too? eg: @domail.co^ such that @domain.co.uk will be skipped? or should we substitute the end with $?

            – Joney Walker
            Jan 28 at 11:32













          • @JoneyWalker No, you probably want $. Please research regular expressions.

            – Kamil Maciorowski
            Jan 28 at 11:38











          • Ok got it... This works: grep -vf <(sed 's/$/$/' Test2.txt) Text1.txt

            – Joney Walker
            Jan 28 at 11:49














          2












          2








          2







          The second file should look like this:



          ^sale@
          ^info@
          ^all@
          ^help@


          because ^ matches the beginning of a line.



          You can rebuild the file with sed -i 's/^/^/' Role.txt. To be clear: this will change the file. Then your original command



          grep -vf Role.txt Emails.txt


          will work.





          Alternatively, with your original Role.txt, in a shell that supports process substitution (e.g. Bash):



          grep -vf <(sed 's/^/^/' Role.txt) Emails.txt


          In this case sed adds ^ characters on the fly and Role.txt remains unchanged.






          share|improve this answer













          The second file should look like this:



          ^sale@
          ^info@
          ^all@
          ^help@


          because ^ matches the beginning of a line.



          You can rebuild the file with sed -i 's/^/^/' Role.txt. To be clear: this will change the file. Then your original command



          grep -vf Role.txt Emails.txt


          will work.





          Alternatively, with your original Role.txt, in a shell that supports process substitution (e.g. Bash):



          grep -vf <(sed 's/^/^/' Role.txt) Emails.txt


          In this case sed adds ^ characters on the fly and Role.txt remains unchanged.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 28 at 10:38









          Kamil MaciorowskiKamil Maciorowski

          29.1k156287




          29.1k156287













          • Perfect! This works great. :)

            – Joney Walker
            Jan 28 at 10:57











          • So if I want to do the same at the end of file, will this work if i add ^ at the end of line too? eg: @domail.co^ such that @domain.co.uk will be skipped? or should we substitute the end with $?

            – Joney Walker
            Jan 28 at 11:32













          • @JoneyWalker No, you probably want $. Please research regular expressions.

            – Kamil Maciorowski
            Jan 28 at 11:38











          • Ok got it... This works: grep -vf <(sed 's/$/$/' Test2.txt) Text1.txt

            – Joney Walker
            Jan 28 at 11:49



















          • Perfect! This works great. :)

            – Joney Walker
            Jan 28 at 10:57











          • So if I want to do the same at the end of file, will this work if i add ^ at the end of line too? eg: @domail.co^ such that @domain.co.uk will be skipped? or should we substitute the end with $?

            – Joney Walker
            Jan 28 at 11:32













          • @JoneyWalker No, you probably want $. Please research regular expressions.

            – Kamil Maciorowski
            Jan 28 at 11:38











          • Ok got it... This works: grep -vf <(sed 's/$/$/' Test2.txt) Text1.txt

            – Joney Walker
            Jan 28 at 11:49

















          Perfect! This works great. :)

          – Joney Walker
          Jan 28 at 10:57





          Perfect! This works great. :)

          – Joney Walker
          Jan 28 at 10:57













          So if I want to do the same at the end of file, will this work if i add ^ at the end of line too? eg: @domail.co^ such that @domain.co.uk will be skipped? or should we substitute the end with $?

          – Joney Walker
          Jan 28 at 11:32







          So if I want to do the same at the end of file, will this work if i add ^ at the end of line too? eg: @domail.co^ such that @domain.co.uk will be skipped? or should we substitute the end with $?

          – Joney Walker
          Jan 28 at 11:32















          @JoneyWalker No, you probably want $. Please research regular expressions.

          – Kamil Maciorowski
          Jan 28 at 11:38





          @JoneyWalker No, you probably want $. Please research regular expressions.

          – Kamil Maciorowski
          Jan 28 at 11:38













          Ok got it... This works: grep -vf <(sed 's/$/$/' Test2.txt) Text1.txt

          – Joney Walker
          Jan 28 at 11:49





          Ok got it... This works: grep -vf <(sed 's/$/$/' Test2.txt) Text1.txt

          – Joney Walker
          Jan 28 at 11:49


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Super User!


          • 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%2fsuperuser.com%2fquestions%2f1399185%2fhow-do-i-delete-lines-of-1st-file-if-it-only-exact-matche-of-the-string-present%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

          If I really need a card on my start hand, how many mulligans make sense? [duplicate]

          Alcedinidae

          Can an atomic nucleus contain both particles and antiparticles? [duplicate]