Find group of words in a text file and extract the line to new text file












0














my file ref.txt contains word "hi" "hello" "aloha" as per below:



hi
hello
aloha


And I have one more file abc.txt which contains many words including the above 3 words.



Now I developed a powershell batch to search the worlds in abc.txt and extract the line containing the words to a new file done.txt. i use -match command to find the word.



How to use the file ref.txt which contains the words for the finding, instead of declare the words in coding? Its fine if its in cmd.exe or powershell coding. Kindly enlighten me.



$source = "C:tempabc.txt"  
$destination = "C:tempdone.txt"
$hits = select-string -Path $source -SimpleMatch "hi","hello","aloha"
$filecontents = get-content $source
foreach($hit in $hits) {
$filecontents[$hit.linenumber-1]| out-file -append
$destination "" |out-file -append $destination
}









share|improve this question
























  • I don't see how a SimpleMatch helps you. I don't think you want "hi" to match "hill". I suspect you only want to match whole words.
    – dbenham
    Mar 20 '15 at 14:19
















0














my file ref.txt contains word "hi" "hello" "aloha" as per below:



hi
hello
aloha


And I have one more file abc.txt which contains many words including the above 3 words.



Now I developed a powershell batch to search the worlds in abc.txt and extract the line containing the words to a new file done.txt. i use -match command to find the word.



How to use the file ref.txt which contains the words for the finding, instead of declare the words in coding? Its fine if its in cmd.exe or powershell coding. Kindly enlighten me.



$source = "C:tempabc.txt"  
$destination = "C:tempdone.txt"
$hits = select-string -Path $source -SimpleMatch "hi","hello","aloha"
$filecontents = get-content $source
foreach($hit in $hits) {
$filecontents[$hit.linenumber-1]| out-file -append
$destination "" |out-file -append $destination
}









share|improve this question
























  • I don't see how a SimpleMatch helps you. I don't think you want "hi" to match "hill". I suspect you only want to match whole words.
    – dbenham
    Mar 20 '15 at 14:19














0












0








0







my file ref.txt contains word "hi" "hello" "aloha" as per below:



hi
hello
aloha


And I have one more file abc.txt which contains many words including the above 3 words.



Now I developed a powershell batch to search the worlds in abc.txt and extract the line containing the words to a new file done.txt. i use -match command to find the word.



How to use the file ref.txt which contains the words for the finding, instead of declare the words in coding? Its fine if its in cmd.exe or powershell coding. Kindly enlighten me.



$source = "C:tempabc.txt"  
$destination = "C:tempdone.txt"
$hits = select-string -Path $source -SimpleMatch "hi","hello","aloha"
$filecontents = get-content $source
foreach($hit in $hits) {
$filecontents[$hit.linenumber-1]| out-file -append
$destination "" |out-file -append $destination
}









share|improve this question















my file ref.txt contains word "hi" "hello" "aloha" as per below:



hi
hello
aloha


And I have one more file abc.txt which contains many words including the above 3 words.



Now I developed a powershell batch to search the worlds in abc.txt and extract the line containing the words to a new file done.txt. i use -match command to find the word.



How to use the file ref.txt which contains the words for the finding, instead of declare the words in coding? Its fine if its in cmd.exe or powershell coding. Kindly enlighten me.



$source = "C:tempabc.txt"  
$destination = "C:tempdone.txt"
$hits = select-string -Path $source -SimpleMatch "hi","hello","aloha"
$filecontents = get-content $source
foreach($hit in $hits) {
$filecontents[$hit.linenumber-1]| out-file -append
$destination "" |out-file -append $destination
}






powershell batch-file






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 20 '15 at 14:11

























asked Mar 20 '15 at 13:56









Poobalan

1115




1115












  • I don't see how a SimpleMatch helps you. I don't think you want "hi" to match "hill". I suspect you only want to match whole words.
    – dbenham
    Mar 20 '15 at 14:19


















  • I don't see how a SimpleMatch helps you. I don't think you want "hi" to match "hill". I suspect you only want to match whole words.
    – dbenham
    Mar 20 '15 at 14:19
















I don't see how a SimpleMatch helps you. I don't think you want "hi" to match "hill". I suspect you only want to match whole words.
– dbenham
Mar 20 '15 at 14:19




I don't see how a SimpleMatch helps you. I don't think you want "hi" to match "hill". I suspect you only want to match whole words.
– dbenham
Mar 20 '15 at 14:19










2 Answers
2






active

oldest

votes


















0














From what I understand in your question, you want to use the words in ref.txt as search criteria, so that if you have a file abc.txt that contains these words, the lines containing the words will be returned in a file done.txt. This can be accomplished simply in cmd.exe in one of a few ways.



findstr /g:ref.txt abc.txt > done.txt


The above solution would return any line that contains any of the words (even if they are in a sentence or part of another word, such as hi and high), but would not be case insensitive.



/R - Use regex
/I - Case insensitive search
/B - Match at the beginning of the line only
/E - Match at the end of the line only


So if you only wanted to match words in the file and not match a word in the sentence, you could do
findstr /BE /g:ref.txt abc.txt > done.txt






share|improve this answer





























    0














    You can use powershell command get-content



    It will get you a string that you can split on n to get each line separately.



    PS C:> get-content c:ref.txt
    hi
    hello
    aloha


    Split it and save in an array. Then perform action of your choice for each item by using



    foreach ($element in $array) 
    {
    /your code here/
    }


    You can find more information here.



    If you want to use all three words together for search match, just replace the n with , and place in your command after -SimpleMatch






    share|improve this answer





















      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%2f891914%2ffind-group-of-words-in-a-text-file-and-extract-the-line-to-new-text-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









      0














      From what I understand in your question, you want to use the words in ref.txt as search criteria, so that if you have a file abc.txt that contains these words, the lines containing the words will be returned in a file done.txt. This can be accomplished simply in cmd.exe in one of a few ways.



      findstr /g:ref.txt abc.txt > done.txt


      The above solution would return any line that contains any of the words (even if they are in a sentence or part of another word, such as hi and high), but would not be case insensitive.



      /R - Use regex
      /I - Case insensitive search
      /B - Match at the beginning of the line only
      /E - Match at the end of the line only


      So if you only wanted to match words in the file and not match a word in the sentence, you could do
      findstr /BE /g:ref.txt abc.txt > done.txt






      share|improve this answer


























        0














        From what I understand in your question, you want to use the words in ref.txt as search criteria, so that if you have a file abc.txt that contains these words, the lines containing the words will be returned in a file done.txt. This can be accomplished simply in cmd.exe in one of a few ways.



        findstr /g:ref.txt abc.txt > done.txt


        The above solution would return any line that contains any of the words (even if they are in a sentence or part of another word, such as hi and high), but would not be case insensitive.



        /R - Use regex
        /I - Case insensitive search
        /B - Match at the beginning of the line only
        /E - Match at the end of the line only


        So if you only wanted to match words in the file and not match a word in the sentence, you could do
        findstr /BE /g:ref.txt abc.txt > done.txt






        share|improve this answer
























          0












          0








          0






          From what I understand in your question, you want to use the words in ref.txt as search criteria, so that if you have a file abc.txt that contains these words, the lines containing the words will be returned in a file done.txt. This can be accomplished simply in cmd.exe in one of a few ways.



          findstr /g:ref.txt abc.txt > done.txt


          The above solution would return any line that contains any of the words (even if they are in a sentence or part of another word, such as hi and high), but would not be case insensitive.



          /R - Use regex
          /I - Case insensitive search
          /B - Match at the beginning of the line only
          /E - Match at the end of the line only


          So if you only wanted to match words in the file and not match a word in the sentence, you could do
          findstr /BE /g:ref.txt abc.txt > done.txt






          share|improve this answer












          From what I understand in your question, you want to use the words in ref.txt as search criteria, so that if you have a file abc.txt that contains these words, the lines containing the words will be returned in a file done.txt. This can be accomplished simply in cmd.exe in one of a few ways.



          findstr /g:ref.txt abc.txt > done.txt


          The above solution would return any line that contains any of the words (even if they are in a sentence or part of another word, such as hi and high), but would not be case insensitive.



          /R - Use regex
          /I - Case insensitive search
          /B - Match at the beginning of the line only
          /E - Match at the end of the line only


          So if you only wanted to match words in the file and not match a word in the sentence, you could do
          findstr /BE /g:ref.txt abc.txt > done.txt







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 20 '15 at 14:21









          Wolvendeer

          1064




          1064

























              0














              You can use powershell command get-content



              It will get you a string that you can split on n to get each line separately.



              PS C:> get-content c:ref.txt
              hi
              hello
              aloha


              Split it and save in an array. Then perform action of your choice for each item by using



              foreach ($element in $array) 
              {
              /your code here/
              }


              You can find more information here.



              If you want to use all three words together for search match, just replace the n with , and place in your command after -SimpleMatch






              share|improve this answer


























                0














                You can use powershell command get-content



                It will get you a string that you can split on n to get each line separately.



                PS C:> get-content c:ref.txt
                hi
                hello
                aloha


                Split it and save in an array. Then perform action of your choice for each item by using



                foreach ($element in $array) 
                {
                /your code here/
                }


                You can find more information here.



                If you want to use all three words together for search match, just replace the n with , and place in your command after -SimpleMatch






                share|improve this answer
























                  0












                  0








                  0






                  You can use powershell command get-content



                  It will get you a string that you can split on n to get each line separately.



                  PS C:> get-content c:ref.txt
                  hi
                  hello
                  aloha


                  Split it and save in an array. Then perform action of your choice for each item by using



                  foreach ($element in $array) 
                  {
                  /your code here/
                  }


                  You can find more information here.



                  If you want to use all three words together for search match, just replace the n with , and place in your command after -SimpleMatch






                  share|improve this answer












                  You can use powershell command get-content



                  It will get you a string that you can split on n to get each line separately.



                  PS C:> get-content c:ref.txt
                  hi
                  hello
                  aloha


                  Split it and save in an array. Then perform action of your choice for each item by using



                  foreach ($element in $array) 
                  {
                  /your code here/
                  }


                  You can find more information here.



                  If you want to use all three words together for search match, just replace the n with , and place in your command after -SimpleMatch







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 20 '15 at 14:24









                  mnmnc

                  3,31311224




                  3,31311224






























                      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.





                      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%2fsuperuser.com%2fquestions%2f891914%2ffind-group-of-words-in-a-text-file-and-extract-the-line-to-new-text-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

                      AnyDesk - Fatal Program Failure

                      How to calibrate 16:9 built-in touch-screen to a 4:3 resolution?

                      Актюбинская область