sed - * works, + doesnt?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I'm renaming a bunch of music files, stripping of the track numbers using sed. However I'm puzzled by sed's behavior towards the '+' character for regex expressions.



What I wanted to do:



for f in *-[0-9]**; do echo "$f" | sed 's/-[0-9]+/-/g'; done


But that didn't work as intended. This workaround did the job:



for f in *-[0-9]**; do echo "$f" | sed 's/-[0-9][0-9]*/-/g'; done


However I would like to know what the issue is with using '+' in sed... Any ideas?







share|improve this question























    up vote
    0
    down vote

    favorite












    I'm renaming a bunch of music files, stripping of the track numbers using sed. However I'm puzzled by sed's behavior towards the '+' character for regex expressions.



    What I wanted to do:



    for f in *-[0-9]**; do echo "$f" | sed 's/-[0-9]+/-/g'; done


    But that didn't work as intended. This workaround did the job:



    for f in *-[0-9]**; do echo "$f" | sed 's/-[0-9][0-9]*/-/g'; done


    However I would like to know what the issue is with using '+' in sed... Any ideas?







    share|improve this question





















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm renaming a bunch of music files, stripping of the track numbers using sed. However I'm puzzled by sed's behavior towards the '+' character for regex expressions.



      What I wanted to do:



      for f in *-[0-9]**; do echo "$f" | sed 's/-[0-9]+/-/g'; done


      But that didn't work as intended. This workaround did the job:



      for f in *-[0-9]**; do echo "$f" | sed 's/-[0-9][0-9]*/-/g'; done


      However I would like to know what the issue is with using '+' in sed... Any ideas?







      share|improve this question











      I'm renaming a bunch of music files, stripping of the track numbers using sed. However I'm puzzled by sed's behavior towards the '+' character for regex expressions.



      What I wanted to do:



      for f in *-[0-9]**; do echo "$f" | sed 's/-[0-9]+/-/g'; done


      But that didn't work as intended. This workaround did the job:



      for f in *-[0-9]**; do echo "$f" | sed 's/-[0-9][0-9]*/-/g'; done


      However I would like to know what the issue is with using '+' in sed... Any ideas?









      share|improve this question










      share|improve this question




      share|improve this question









      asked Jun 27 at 11:45









      koen

      1




      1




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote













          + is an extended regular expression modifier. sed does basic regular expressions by default.



          To make sed parse your expressions as extended regular expressions, use sed -E. GNU sed also understands + in basic regular expressions if you use it as +.



          Also, your pattern *-[0-9]** looks strange. The - does not need escaping (not in the shell pattern and not in the regular expression), and unless you use globstar in bash, the ** pattern will expand to itself. With globstar enabled in bash *-[0-9]** would expand to all pathnames that contain -N in the first filename component (where N is a digit).



          What I think you'd like to do is something like this (probably not quite, but I don't know what your filenames look like):



          for name in *-[0-9][0-9]*; do
          newname=$name%-*
          printf 'Would rename "%s" into "%s"n' "$name" "$newname"
          # mv "$name" "$newname"
          done


          This matches all names in the current directory that contains -NN (N is a digit). It strips off everything from the last - in the filename and renames the file (the actual renaming is commented out).






          share|improve this answer



















          • 1




            Ah yes, escaping is a GNU-ism...
            – Stephen Kitt
            Jun 27 at 11:53










          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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          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%2f452202%2fsed-works-doesnt%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote













          + is an extended regular expression modifier. sed does basic regular expressions by default.



          To make sed parse your expressions as extended regular expressions, use sed -E. GNU sed also understands + in basic regular expressions if you use it as +.



          Also, your pattern *-[0-9]** looks strange. The - does not need escaping (not in the shell pattern and not in the regular expression), and unless you use globstar in bash, the ** pattern will expand to itself. With globstar enabled in bash *-[0-9]** would expand to all pathnames that contain -N in the first filename component (where N is a digit).



          What I think you'd like to do is something like this (probably not quite, but I don't know what your filenames look like):



          for name in *-[0-9][0-9]*; do
          newname=$name%-*
          printf 'Would rename "%s" into "%s"n' "$name" "$newname"
          # mv "$name" "$newname"
          done


          This matches all names in the current directory that contains -NN (N is a digit). It strips off everything from the last - in the filename and renames the file (the actual renaming is commented out).






          share|improve this answer



















          • 1




            Ah yes, escaping is a GNU-ism...
            – Stephen Kitt
            Jun 27 at 11:53














          up vote
          3
          down vote













          + is an extended regular expression modifier. sed does basic regular expressions by default.



          To make sed parse your expressions as extended regular expressions, use sed -E. GNU sed also understands + in basic regular expressions if you use it as +.



          Also, your pattern *-[0-9]** looks strange. The - does not need escaping (not in the shell pattern and not in the regular expression), and unless you use globstar in bash, the ** pattern will expand to itself. With globstar enabled in bash *-[0-9]** would expand to all pathnames that contain -N in the first filename component (where N is a digit).



          What I think you'd like to do is something like this (probably not quite, but I don't know what your filenames look like):



          for name in *-[0-9][0-9]*; do
          newname=$name%-*
          printf 'Would rename "%s" into "%s"n' "$name" "$newname"
          # mv "$name" "$newname"
          done


          This matches all names in the current directory that contains -NN (N is a digit). It strips off everything from the last - in the filename and renames the file (the actual renaming is commented out).






          share|improve this answer



















          • 1




            Ah yes, escaping is a GNU-ism...
            – Stephen Kitt
            Jun 27 at 11:53












          up vote
          3
          down vote










          up vote
          3
          down vote









          + is an extended regular expression modifier. sed does basic regular expressions by default.



          To make sed parse your expressions as extended regular expressions, use sed -E. GNU sed also understands + in basic regular expressions if you use it as +.



          Also, your pattern *-[0-9]** looks strange. The - does not need escaping (not in the shell pattern and not in the regular expression), and unless you use globstar in bash, the ** pattern will expand to itself. With globstar enabled in bash *-[0-9]** would expand to all pathnames that contain -N in the first filename component (where N is a digit).



          What I think you'd like to do is something like this (probably not quite, but I don't know what your filenames look like):



          for name in *-[0-9][0-9]*; do
          newname=$name%-*
          printf 'Would rename "%s" into "%s"n' "$name" "$newname"
          # mv "$name" "$newname"
          done


          This matches all names in the current directory that contains -NN (N is a digit). It strips off everything from the last - in the filename and renames the file (the actual renaming is commented out).






          share|improve this answer















          + is an extended regular expression modifier. sed does basic regular expressions by default.



          To make sed parse your expressions as extended regular expressions, use sed -E. GNU sed also understands + in basic regular expressions if you use it as +.



          Also, your pattern *-[0-9]** looks strange. The - does not need escaping (not in the shell pattern and not in the regular expression), and unless you use globstar in bash, the ** pattern will expand to itself. With globstar enabled in bash *-[0-9]** would expand to all pathnames that contain -N in the first filename component (where N is a digit).



          What I think you'd like to do is something like this (probably not quite, but I don't know what your filenames look like):



          for name in *-[0-9][0-9]*; do
          newname=$name%-*
          printf 'Would rename "%s" into "%s"n' "$name" "$newname"
          # mv "$name" "$newname"
          done


          This matches all names in the current directory that contains -NN (N is a digit). It strips off everything from the last - in the filename and renames the file (the actual renaming is commented out).







          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Jun 27 at 12:00


























          answered Jun 27 at 11:48









          Kusalananda

          101k13199312




          101k13199312







          • 1




            Ah yes, escaping is a GNU-ism...
            – Stephen Kitt
            Jun 27 at 11:53












          • 1




            Ah yes, escaping is a GNU-ism...
            – Stephen Kitt
            Jun 27 at 11:53







          1




          1




          Ah yes, escaping is a GNU-ism...
          – Stephen Kitt
          Jun 27 at 11:53




          Ah yes, escaping is a GNU-ism...
          – Stephen Kitt
          Jun 27 at 11:53












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f452202%2fsed-works-doesnt%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?