How to open files which have spaces in filenames with grep?












1















I would like to search for phrase inside .txt files and with Sublime text open all files which are found. I am using Git Bash for Windows and after various experimenting this is what I have in .bashrc



# Alias for opening Sublime Text
alias subl='C:/Program Files/Sublime Text 3/sublime_text.exe'

# Search inside files and open them in Sublime Text
lookfor() {
subl $(grep -rIl "$1" /c/wamp64/www/notes --exclude-dir=".git")
}


I use the script like this



lookfor lorem ipsum


The script works only for filenames which don't have space, if they do then Sublime will open a new file for each of those words in filename.




  1. How can the script correctly open all files that grep finds?

  2. How can the script stop executing itself after opening Sublime Text and give me back control in bash? Currently I need to close Sublime and only then bash will be available again


I have tried other various code for opening files with no success



grep -rlZ "lorem ipsum" ./* | xargs -0 subl
xargs: subl: No such file or directory









share|improve this question



























    1















    I would like to search for phrase inside .txt files and with Sublime text open all files which are found. I am using Git Bash for Windows and after various experimenting this is what I have in .bashrc



    # Alias for opening Sublime Text
    alias subl='C:/Program Files/Sublime Text 3/sublime_text.exe'

    # Search inside files and open them in Sublime Text
    lookfor() {
    subl $(grep -rIl "$1" /c/wamp64/www/notes --exclude-dir=".git")
    }


    I use the script like this



    lookfor lorem ipsum


    The script works only for filenames which don't have space, if they do then Sublime will open a new file for each of those words in filename.




    1. How can the script correctly open all files that grep finds?

    2. How can the script stop executing itself after opening Sublime Text and give me back control in bash? Currently I need to close Sublime and only then bash will be available again


    I have tried other various code for opening files with no success



    grep -rlZ "lorem ipsum" ./* | xargs -0 subl
    xargs: subl: No such file or directory









    share|improve this question

























      1












      1








      1








      I would like to search for phrase inside .txt files and with Sublime text open all files which are found. I am using Git Bash for Windows and after various experimenting this is what I have in .bashrc



      # Alias for opening Sublime Text
      alias subl='C:/Program Files/Sublime Text 3/sublime_text.exe'

      # Search inside files and open them in Sublime Text
      lookfor() {
      subl $(grep -rIl "$1" /c/wamp64/www/notes --exclude-dir=".git")
      }


      I use the script like this



      lookfor lorem ipsum


      The script works only for filenames which don't have space, if they do then Sublime will open a new file for each of those words in filename.




      1. How can the script correctly open all files that grep finds?

      2. How can the script stop executing itself after opening Sublime Text and give me back control in bash? Currently I need to close Sublime and only then bash will be available again


      I have tried other various code for opening files with no success



      grep -rlZ "lorem ipsum" ./* | xargs -0 subl
      xargs: subl: No such file or directory









      share|improve this question














      I would like to search for phrase inside .txt files and with Sublime text open all files which are found. I am using Git Bash for Windows and after various experimenting this is what I have in .bashrc



      # Alias for opening Sublime Text
      alias subl='C:/Program Files/Sublime Text 3/sublime_text.exe'

      # Search inside files and open them in Sublime Text
      lookfor() {
      subl $(grep -rIl "$1" /c/wamp64/www/notes --exclude-dir=".git")
      }


      I use the script like this



      lookfor lorem ipsum


      The script works only for filenames which don't have space, if they do then Sublime will open a new file for each of those words in filename.




      1. How can the script correctly open all files that grep finds?

      2. How can the script stop executing itself after opening Sublime Text and give me back control in bash? Currently I need to close Sublime and only then bash will be available again


      I have tried other various code for opening files with no success



      grep -rlZ "lorem ipsum" ./* | xargs -0 subl
      xargs: subl: No such file or directory






      windows-10 bash grep






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 30 '18 at 22:52









      Ivan TopićIvan Topić

      1084




      1084






















          1 Answer
          1






          active

          oldest

          votes


















          3














          This question boils down to the fact, that shell parameters use unescaped whitespace as a separator, so just cycling through them is tricky.



          The IMHO best way is to treat newlines and whitspace differently:



          lookup() {
          while read f ; do
          test -f "$f" && subl "$f"
          done < <(grep -rIl "$1" /c/wamp64/www/notes --exclude-dir=".git")
          }


          So you read the results line by line instead of token by token and thus keep different tokens of the same filename together.



          EDIT



          For applications, where opening the files in one go is different to opening the one-by-one this can easily be adapted:



          lookup() {
          CMD="subl"
          while read f ; do
          test -f "$f" && CMD="$CMD '$f'"
          done < <(grep -rIl "$1" /c/wamp64/www/notes --exclude-dir=".git")
          eval "$CMD"
          }





          share|improve this answer


























          • I think this works for this particular case since Sublime just opens the files in the same window. Other commands may not work this way and require the list of files as one set of arguments.

            – slhck
            Dec 30 '18 at 23:25











          • Thank you, this opens all files separately - first opens one file, then after I close it and Sublime, it reopens Sublime with a second file etc, so the only downside is that it is a little slower for workflow but it gets the job done

            – Ivan Topić
            Dec 30 '18 at 23:27











          • See my edit for this use case

            – Eugen Rieck
            Dec 30 '18 at 23:44













          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%2f1389088%2fhow-to-open-files-which-have-spaces-in-filenames-with-grep%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









          3














          This question boils down to the fact, that shell parameters use unescaped whitespace as a separator, so just cycling through them is tricky.



          The IMHO best way is to treat newlines and whitspace differently:



          lookup() {
          while read f ; do
          test -f "$f" && subl "$f"
          done < <(grep -rIl "$1" /c/wamp64/www/notes --exclude-dir=".git")
          }


          So you read the results line by line instead of token by token and thus keep different tokens of the same filename together.



          EDIT



          For applications, where opening the files in one go is different to opening the one-by-one this can easily be adapted:



          lookup() {
          CMD="subl"
          while read f ; do
          test -f "$f" && CMD="$CMD '$f'"
          done < <(grep -rIl "$1" /c/wamp64/www/notes --exclude-dir=".git")
          eval "$CMD"
          }





          share|improve this answer


























          • I think this works for this particular case since Sublime just opens the files in the same window. Other commands may not work this way and require the list of files as one set of arguments.

            – slhck
            Dec 30 '18 at 23:25











          • Thank you, this opens all files separately - first opens one file, then after I close it and Sublime, it reopens Sublime with a second file etc, so the only downside is that it is a little slower for workflow but it gets the job done

            – Ivan Topić
            Dec 30 '18 at 23:27











          • See my edit for this use case

            – Eugen Rieck
            Dec 30 '18 at 23:44


















          3














          This question boils down to the fact, that shell parameters use unescaped whitespace as a separator, so just cycling through them is tricky.



          The IMHO best way is to treat newlines and whitspace differently:



          lookup() {
          while read f ; do
          test -f "$f" && subl "$f"
          done < <(grep -rIl "$1" /c/wamp64/www/notes --exclude-dir=".git")
          }


          So you read the results line by line instead of token by token and thus keep different tokens of the same filename together.



          EDIT



          For applications, where opening the files in one go is different to opening the one-by-one this can easily be adapted:



          lookup() {
          CMD="subl"
          while read f ; do
          test -f "$f" && CMD="$CMD '$f'"
          done < <(grep -rIl "$1" /c/wamp64/www/notes --exclude-dir=".git")
          eval "$CMD"
          }





          share|improve this answer


























          • I think this works for this particular case since Sublime just opens the files in the same window. Other commands may not work this way and require the list of files as one set of arguments.

            – slhck
            Dec 30 '18 at 23:25











          • Thank you, this opens all files separately - first opens one file, then after I close it and Sublime, it reopens Sublime with a second file etc, so the only downside is that it is a little slower for workflow but it gets the job done

            – Ivan Topić
            Dec 30 '18 at 23:27











          • See my edit for this use case

            – Eugen Rieck
            Dec 30 '18 at 23:44
















          3












          3








          3







          This question boils down to the fact, that shell parameters use unescaped whitespace as a separator, so just cycling through them is tricky.



          The IMHO best way is to treat newlines and whitspace differently:



          lookup() {
          while read f ; do
          test -f "$f" && subl "$f"
          done < <(grep -rIl "$1" /c/wamp64/www/notes --exclude-dir=".git")
          }


          So you read the results line by line instead of token by token and thus keep different tokens of the same filename together.



          EDIT



          For applications, where opening the files in one go is different to opening the one-by-one this can easily be adapted:



          lookup() {
          CMD="subl"
          while read f ; do
          test -f "$f" && CMD="$CMD '$f'"
          done < <(grep -rIl "$1" /c/wamp64/www/notes --exclude-dir=".git")
          eval "$CMD"
          }





          share|improve this answer















          This question boils down to the fact, that shell parameters use unescaped whitespace as a separator, so just cycling through them is tricky.



          The IMHO best way is to treat newlines and whitspace differently:



          lookup() {
          while read f ; do
          test -f "$f" && subl "$f"
          done < <(grep -rIl "$1" /c/wamp64/www/notes --exclude-dir=".git")
          }


          So you read the results line by line instead of token by token and thus keep different tokens of the same filename together.



          EDIT



          For applications, where opening the files in one go is different to opening the one-by-one this can easily be adapted:



          lookup() {
          CMD="subl"
          while read f ; do
          test -f "$f" && CMD="$CMD '$f'"
          done < <(grep -rIl "$1" /c/wamp64/www/notes --exclude-dir=".git")
          eval "$CMD"
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 30 '18 at 23:44

























          answered Dec 30 '18 at 23:03









          Eugen RieckEugen Rieck

          10.1k22128




          10.1k22128













          • I think this works for this particular case since Sublime just opens the files in the same window. Other commands may not work this way and require the list of files as one set of arguments.

            – slhck
            Dec 30 '18 at 23:25











          • Thank you, this opens all files separately - first opens one file, then after I close it and Sublime, it reopens Sublime with a second file etc, so the only downside is that it is a little slower for workflow but it gets the job done

            – Ivan Topić
            Dec 30 '18 at 23:27











          • See my edit for this use case

            – Eugen Rieck
            Dec 30 '18 at 23:44





















          • I think this works for this particular case since Sublime just opens the files in the same window. Other commands may not work this way and require the list of files as one set of arguments.

            – slhck
            Dec 30 '18 at 23:25











          • Thank you, this opens all files separately - first opens one file, then after I close it and Sublime, it reopens Sublime with a second file etc, so the only downside is that it is a little slower for workflow but it gets the job done

            – Ivan Topić
            Dec 30 '18 at 23:27











          • See my edit for this use case

            – Eugen Rieck
            Dec 30 '18 at 23:44



















          I think this works for this particular case since Sublime just opens the files in the same window. Other commands may not work this way and require the list of files as one set of arguments.

          – slhck
          Dec 30 '18 at 23:25





          I think this works for this particular case since Sublime just opens the files in the same window. Other commands may not work this way and require the list of files as one set of arguments.

          – slhck
          Dec 30 '18 at 23:25













          Thank you, this opens all files separately - first opens one file, then after I close it and Sublime, it reopens Sublime with a second file etc, so the only downside is that it is a little slower for workflow but it gets the job done

          – Ivan Topić
          Dec 30 '18 at 23:27





          Thank you, this opens all files separately - first opens one file, then after I close it and Sublime, it reopens Sublime with a second file etc, so the only downside is that it is a little slower for workflow but it gets the job done

          – Ivan Topić
          Dec 30 '18 at 23:27













          See my edit for this use case

          – Eugen Rieck
          Dec 30 '18 at 23:44







          See my edit for this use case

          – Eugen Rieck
          Dec 30 '18 at 23:44




















          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%2f1389088%2fhow-to-open-files-which-have-spaces-in-filenames-with-grep%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

          Paul Cézanne

          UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

          Angular material date-picker (MatDatepicker) auto completes the date on focus out