Mac terminal : Sed regex, extracting part of a string












0















I have a string , exactly a file path



/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt,



I need to extract the path of its folder ,



to get /Users/dengjiangzhou/Documents/源码/198/ .



Here is what I tried.



echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt " | sed  's@.*(/.+)@1@p' 

/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt


I use regex capture group , try to capture group one /李宏毅-机器学习2017秋季.txt, and replace it with ,



Seems like that regex capture group have the effect of matching from the end to beginning.



then I will get



/Users/dengjiangzhou/Documents/源码/198


I have read Sed regex, extracting part of a string in Mac terminal again and again.



Hope get helped, amigo



Really want to get /Users/dengjiangzhou/Documents/源码/198/,



/Users/dengjiangzhou/Documents/源码/198 is not good.



any good idea?










share|improve this question



























    0















    I have a string , exactly a file path



    /Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt,



    I need to extract the path of its folder ,



    to get /Users/dengjiangzhou/Documents/源码/198/ .



    Here is what I tried.



    echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt " | sed  's@.*(/.+)@1@p' 

    /Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt


    I use regex capture group , try to capture group one /李宏毅-机器学习2017秋季.txt, and replace it with ,



    Seems like that regex capture group have the effect of matching from the end to beginning.



    then I will get



    /Users/dengjiangzhou/Documents/源码/198


    I have read Sed regex, extracting part of a string in Mac terminal again and again.



    Hope get helped, amigo



    Really want to get /Users/dengjiangzhou/Documents/源码/198/,



    /Users/dengjiangzhou/Documents/源码/198 is not good.



    any good idea?










    share|improve this question

























      0












      0








      0








      I have a string , exactly a file path



      /Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt,



      I need to extract the path of its folder ,



      to get /Users/dengjiangzhou/Documents/源码/198/ .



      Here is what I tried.



      echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt " | sed  's@.*(/.+)@1@p' 

      /Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt


      I use regex capture group , try to capture group one /李宏毅-机器学习2017秋季.txt, and replace it with ,



      Seems like that regex capture group have the effect of matching from the end to beginning.



      then I will get



      /Users/dengjiangzhou/Documents/源码/198


      I have read Sed regex, extracting part of a string in Mac terminal again and again.



      Hope get helped, amigo



      Really want to get /Users/dengjiangzhou/Documents/源码/198/,



      /Users/dengjiangzhou/Documents/源码/198 is not good.



      any good idea?










      share|improve this question














      I have a string , exactly a file path



      /Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt,



      I need to extract the path of its folder ,



      to get /Users/dengjiangzhou/Documents/源码/198/ .



      Here is what I tried.



      echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt " | sed  's@.*(/.+)@1@p' 

      /Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt


      I use regex capture group , try to capture group one /李宏毅-机器学习2017秋季.txt, and replace it with ,



      Seems like that regex capture group have the effect of matching from the end to beginning.



      then I will get



      /Users/dengjiangzhou/Documents/源码/198


      I have read Sed regex, extracting part of a string in Mac terminal again and again.



      Hope get helped, amigo



      Really want to get /Users/dengjiangzhou/Documents/源码/198/,



      /Users/dengjiangzhou/Documents/源码/198 is not good.



      any good idea?







      mac terminal regex sed






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 11 at 2:10









      black_pearlblack_pearl

      1086




      1086






















          1 Answer
          1






          active

          oldest

          votes


















          1














          The first try doesn't work because the escape for + is missing



          echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt " | sed  's@.*(/.+)@1@' 
          /李宏毅-机器学习2017秋季.txt


          (escapes for metacharacters can be avoided with -r or -E option, for Extended RE).



          What you want is the dirname part, then just group the first part of the ER.



          echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt " | sed  's@(.*)/.*@1@' 
          /Users/dengjiangzhou/Documents/源码/198


          The quantifiers in sed are greedy, it means that when you ask to match a regex like .*/.*, the first .* will match all line until the end, because you asked to match anything. in any number*, and a / is anything in any number.



          edit- If you want to read about regular expressions and sed, take a look
          https://www.regular-expressions.info/tutorial.html
          http://www.grymoire.com/Unix/Sed.html#uh-0






          share|improve this answer


























          • The second regex is intuitive and nice. could you tell me why I try the first regex in my terminal echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt" | sed 's@.*(/.+)@1@' result is /Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt. not you showed above

            – black_pearl
            Jan 11 at 3:27








          • 1





            @black_pearl This command line echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt" | sed 's@.*(/.+)@1@' you posted in the comment works, the output is /李宏毅-机器学习2017秋季.txt. Are you running this at MacOS (by the post title)? If yes, we are running different versions of sed, I'm on GNU/Linux. I don't know if all those unicode characters could affect the output. Could you try only with ASCII to see if it works echo "/Users/dengjiangzhou/Documents/198/file2017.txt" | sed 's@.*(/.+)@1@' results in expected /file2017.txt

            – Paulo
            Jan 11 at 12:01











          • I run in a MacBook. No , still that. echo "/Users/dengjiangzhou/Documents/198/file2017.txt" | sed 's@.*(/.+)@1@' , result is /Users/dengjiangzhou/Documents/198/file2017.txt. I should read your links, and try

            – black_pearl
            Jan 11 at 15:19








          • 1





            This does work? echo "/Users/dengjiangzhou/Documents/198/file2017.txt" | sed -E 's@.*(/.+)@1@'. Try that with the unicode string too please, it should work. I ran in a FreeBSD VirtualBox vm and it worked, but it's language isn't properly configured.

            – Paulo
            Jan 11 at 17:58






          • 1





            It seems that backreference in MacOS belongs to ERE, it needs -E option.

            – Paulo
            Jan 12 at 13:25











          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%2f1392997%2fmac-terminal-sed-regex-extracting-part-of-a-string%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









          1














          The first try doesn't work because the escape for + is missing



          echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt " | sed  's@.*(/.+)@1@' 
          /李宏毅-机器学习2017秋季.txt


          (escapes for metacharacters can be avoided with -r or -E option, for Extended RE).



          What you want is the dirname part, then just group the first part of the ER.



          echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt " | sed  's@(.*)/.*@1@' 
          /Users/dengjiangzhou/Documents/源码/198


          The quantifiers in sed are greedy, it means that when you ask to match a regex like .*/.*, the first .* will match all line until the end, because you asked to match anything. in any number*, and a / is anything in any number.



          edit- If you want to read about regular expressions and sed, take a look
          https://www.regular-expressions.info/tutorial.html
          http://www.grymoire.com/Unix/Sed.html#uh-0






          share|improve this answer


























          • The second regex is intuitive and nice. could you tell me why I try the first regex in my terminal echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt" | sed 's@.*(/.+)@1@' result is /Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt. not you showed above

            – black_pearl
            Jan 11 at 3:27








          • 1





            @black_pearl This command line echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt" | sed 's@.*(/.+)@1@' you posted in the comment works, the output is /李宏毅-机器学习2017秋季.txt. Are you running this at MacOS (by the post title)? If yes, we are running different versions of sed, I'm on GNU/Linux. I don't know if all those unicode characters could affect the output. Could you try only with ASCII to see if it works echo "/Users/dengjiangzhou/Documents/198/file2017.txt" | sed 's@.*(/.+)@1@' results in expected /file2017.txt

            – Paulo
            Jan 11 at 12:01











          • I run in a MacBook. No , still that. echo "/Users/dengjiangzhou/Documents/198/file2017.txt" | sed 's@.*(/.+)@1@' , result is /Users/dengjiangzhou/Documents/198/file2017.txt. I should read your links, and try

            – black_pearl
            Jan 11 at 15:19








          • 1





            This does work? echo "/Users/dengjiangzhou/Documents/198/file2017.txt" | sed -E 's@.*(/.+)@1@'. Try that with the unicode string too please, it should work. I ran in a FreeBSD VirtualBox vm and it worked, but it's language isn't properly configured.

            – Paulo
            Jan 11 at 17:58






          • 1





            It seems that backreference in MacOS belongs to ERE, it needs -E option.

            – Paulo
            Jan 12 at 13:25
















          1














          The first try doesn't work because the escape for + is missing



          echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt " | sed  's@.*(/.+)@1@' 
          /李宏毅-机器学习2017秋季.txt


          (escapes for metacharacters can be avoided with -r or -E option, for Extended RE).



          What you want is the dirname part, then just group the first part of the ER.



          echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt " | sed  's@(.*)/.*@1@' 
          /Users/dengjiangzhou/Documents/源码/198


          The quantifiers in sed are greedy, it means that when you ask to match a regex like .*/.*, the first .* will match all line until the end, because you asked to match anything. in any number*, and a / is anything in any number.



          edit- If you want to read about regular expressions and sed, take a look
          https://www.regular-expressions.info/tutorial.html
          http://www.grymoire.com/Unix/Sed.html#uh-0






          share|improve this answer


























          • The second regex is intuitive and nice. could you tell me why I try the first regex in my terminal echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt" | sed 's@.*(/.+)@1@' result is /Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt. not you showed above

            – black_pearl
            Jan 11 at 3:27








          • 1





            @black_pearl This command line echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt" | sed 's@.*(/.+)@1@' you posted in the comment works, the output is /李宏毅-机器学习2017秋季.txt. Are you running this at MacOS (by the post title)? If yes, we are running different versions of sed, I'm on GNU/Linux. I don't know if all those unicode characters could affect the output. Could you try only with ASCII to see if it works echo "/Users/dengjiangzhou/Documents/198/file2017.txt" | sed 's@.*(/.+)@1@' results in expected /file2017.txt

            – Paulo
            Jan 11 at 12:01











          • I run in a MacBook. No , still that. echo "/Users/dengjiangzhou/Documents/198/file2017.txt" | sed 's@.*(/.+)@1@' , result is /Users/dengjiangzhou/Documents/198/file2017.txt. I should read your links, and try

            – black_pearl
            Jan 11 at 15:19








          • 1





            This does work? echo "/Users/dengjiangzhou/Documents/198/file2017.txt" | sed -E 's@.*(/.+)@1@'. Try that with the unicode string too please, it should work. I ran in a FreeBSD VirtualBox vm and it worked, but it's language isn't properly configured.

            – Paulo
            Jan 11 at 17:58






          • 1





            It seems that backreference in MacOS belongs to ERE, it needs -E option.

            – Paulo
            Jan 12 at 13:25














          1












          1








          1







          The first try doesn't work because the escape for + is missing



          echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt " | sed  's@.*(/.+)@1@' 
          /李宏毅-机器学习2017秋季.txt


          (escapes for metacharacters can be avoided with -r or -E option, for Extended RE).



          What you want is the dirname part, then just group the first part of the ER.



          echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt " | sed  's@(.*)/.*@1@' 
          /Users/dengjiangzhou/Documents/源码/198


          The quantifiers in sed are greedy, it means that when you ask to match a regex like .*/.*, the first .* will match all line until the end, because you asked to match anything. in any number*, and a / is anything in any number.



          edit- If you want to read about regular expressions and sed, take a look
          https://www.regular-expressions.info/tutorial.html
          http://www.grymoire.com/Unix/Sed.html#uh-0






          share|improve this answer















          The first try doesn't work because the escape for + is missing



          echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt " | sed  's@.*(/.+)@1@' 
          /李宏毅-机器学习2017秋季.txt


          (escapes for metacharacters can be avoided with -r or -E option, for Extended RE).



          What you want is the dirname part, then just group the first part of the ER.



          echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt " | sed  's@(.*)/.*@1@' 
          /Users/dengjiangzhou/Documents/源码/198


          The quantifiers in sed are greedy, it means that when you ask to match a regex like .*/.*, the first .* will match all line until the end, because you asked to match anything. in any number*, and a / is anything in any number.



          edit- If you want to read about regular expressions and sed, take a look
          https://www.regular-expressions.info/tutorial.html
          http://www.grymoire.com/Unix/Sed.html#uh-0







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 11 at 3:08

























          answered Jan 11 at 3:03









          PauloPaulo

          57628




          57628













          • The second regex is intuitive and nice. could you tell me why I try the first regex in my terminal echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt" | sed 's@.*(/.+)@1@' result is /Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt. not you showed above

            – black_pearl
            Jan 11 at 3:27








          • 1





            @black_pearl This command line echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt" | sed 's@.*(/.+)@1@' you posted in the comment works, the output is /李宏毅-机器学习2017秋季.txt. Are you running this at MacOS (by the post title)? If yes, we are running different versions of sed, I'm on GNU/Linux. I don't know if all those unicode characters could affect the output. Could you try only with ASCII to see if it works echo "/Users/dengjiangzhou/Documents/198/file2017.txt" | sed 's@.*(/.+)@1@' results in expected /file2017.txt

            – Paulo
            Jan 11 at 12:01











          • I run in a MacBook. No , still that. echo "/Users/dengjiangzhou/Documents/198/file2017.txt" | sed 's@.*(/.+)@1@' , result is /Users/dengjiangzhou/Documents/198/file2017.txt. I should read your links, and try

            – black_pearl
            Jan 11 at 15:19








          • 1





            This does work? echo "/Users/dengjiangzhou/Documents/198/file2017.txt" | sed -E 's@.*(/.+)@1@'. Try that with the unicode string too please, it should work. I ran in a FreeBSD VirtualBox vm and it worked, but it's language isn't properly configured.

            – Paulo
            Jan 11 at 17:58






          • 1





            It seems that backreference in MacOS belongs to ERE, it needs -E option.

            – Paulo
            Jan 12 at 13:25



















          • The second regex is intuitive and nice. could you tell me why I try the first regex in my terminal echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt" | sed 's@.*(/.+)@1@' result is /Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt. not you showed above

            – black_pearl
            Jan 11 at 3:27








          • 1





            @black_pearl This command line echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt" | sed 's@.*(/.+)@1@' you posted in the comment works, the output is /李宏毅-机器学习2017秋季.txt. Are you running this at MacOS (by the post title)? If yes, we are running different versions of sed, I'm on GNU/Linux. I don't know if all those unicode characters could affect the output. Could you try only with ASCII to see if it works echo "/Users/dengjiangzhou/Documents/198/file2017.txt" | sed 's@.*(/.+)@1@' results in expected /file2017.txt

            – Paulo
            Jan 11 at 12:01











          • I run in a MacBook. No , still that. echo "/Users/dengjiangzhou/Documents/198/file2017.txt" | sed 's@.*(/.+)@1@' , result is /Users/dengjiangzhou/Documents/198/file2017.txt. I should read your links, and try

            – black_pearl
            Jan 11 at 15:19








          • 1





            This does work? echo "/Users/dengjiangzhou/Documents/198/file2017.txt" | sed -E 's@.*(/.+)@1@'. Try that with the unicode string too please, it should work. I ran in a FreeBSD VirtualBox vm and it worked, but it's language isn't properly configured.

            – Paulo
            Jan 11 at 17:58






          • 1





            It seems that backreference in MacOS belongs to ERE, it needs -E option.

            – Paulo
            Jan 12 at 13:25

















          The second regex is intuitive and nice. could you tell me why I try the first regex in my terminal echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt" | sed 's@.*(/.+)@1@' result is /Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt. not you showed above

          – black_pearl
          Jan 11 at 3:27







          The second regex is intuitive and nice. could you tell me why I try the first regex in my terminal echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt" | sed 's@.*(/.+)@1@' result is /Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt. not you showed above

          – black_pearl
          Jan 11 at 3:27






          1




          1





          @black_pearl This command line echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt" | sed 's@.*(/.+)@1@' you posted in the comment works, the output is /李宏毅-机器学习2017秋季.txt. Are you running this at MacOS (by the post title)? If yes, we are running different versions of sed, I'm on GNU/Linux. I don't know if all those unicode characters could affect the output. Could you try only with ASCII to see if it works echo "/Users/dengjiangzhou/Documents/198/file2017.txt" | sed 's@.*(/.+)@1@' results in expected /file2017.txt

          – Paulo
          Jan 11 at 12:01





          @black_pearl This command line echo "/Users/dengjiangzhou/Documents/源码/198/李宏毅-机器学习2017秋季.txt" | sed 's@.*(/.+)@1@' you posted in the comment works, the output is /李宏毅-机器学习2017秋季.txt. Are you running this at MacOS (by the post title)? If yes, we are running different versions of sed, I'm on GNU/Linux. I don't know if all those unicode characters could affect the output. Could you try only with ASCII to see if it works echo "/Users/dengjiangzhou/Documents/198/file2017.txt" | sed 's@.*(/.+)@1@' results in expected /file2017.txt

          – Paulo
          Jan 11 at 12:01













          I run in a MacBook. No , still that. echo "/Users/dengjiangzhou/Documents/198/file2017.txt" | sed 's@.*(/.+)@1@' , result is /Users/dengjiangzhou/Documents/198/file2017.txt. I should read your links, and try

          – black_pearl
          Jan 11 at 15:19







          I run in a MacBook. No , still that. echo "/Users/dengjiangzhou/Documents/198/file2017.txt" | sed 's@.*(/.+)@1@' , result is /Users/dengjiangzhou/Documents/198/file2017.txt. I should read your links, and try

          – black_pearl
          Jan 11 at 15:19






          1




          1





          This does work? echo "/Users/dengjiangzhou/Documents/198/file2017.txt" | sed -E 's@.*(/.+)@1@'. Try that with the unicode string too please, it should work. I ran in a FreeBSD VirtualBox vm and it worked, but it's language isn't properly configured.

          – Paulo
          Jan 11 at 17:58





          This does work? echo "/Users/dengjiangzhou/Documents/198/file2017.txt" | sed -E 's@.*(/.+)@1@'. Try that with the unicode string too please, it should work. I ran in a FreeBSD VirtualBox vm and it worked, but it's language isn't properly configured.

          – Paulo
          Jan 11 at 17:58




          1




          1





          It seems that backreference in MacOS belongs to ERE, it needs -E option.

          – Paulo
          Jan 12 at 13:25





          It seems that backreference in MacOS belongs to ERE, it needs -E option.

          – Paulo
          Jan 12 at 13:25


















          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%2f1392997%2fmac-terminal-sed-regex-extracting-part-of-a-string%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

          How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

          is 'sed' thread safe

          How to make a Squid Proxy server?