0

I'm feeling really stupid about asking this. I should know why this is failing. The following command works as expected in linux but not on MacOS Sierra:

echo "ENGINE=MyISAM AUTO_INCREMENT=750 DEFAULT" | sed 's/ AUTO_INCREMENT=[0-9]\+//'

On linux the command results in:

ENGINE=MyISAM DEFAULT

I've tried to various iterations of this command and cannot get it to work. I know that MacOS's sed is the BSD version instead of the GNU version but this substitute command should still work. What am I doing wrong?

EDIT: The suggested duplicate answer does solve my problem but in a densely worded, roundabout way. The TL;DR version is thusly:

MacOS uses BSD-style sed. The solution is to simply use the -E option (extended regular expressions) which doesn't require escaping certain characters (like the + symbol). This method also works just fine with GNU-style sed found on most Linux systems. I hope this helps somebody down the road.

Todd Hammer
  • 183
  • 1
  • 9
  • 1
    Possible duplicate of [sed not giving me correct substitute operation for newline with Mac - differences between GNU sed and BSD / OSX sed](https://stackoverflow.com/questions/24275070/sed-not-giving-me-correct-substitute-operation-for-newline-with-mac-difference) – Sundeep Jul 08 '18 at 15:38
  • 1
    `\+` is the difference.. try `sed -E 's/ AUTO_INCREMENT=[0-9]+//'` – Sundeep Jul 08 '18 at 15:38
  • 1
    Yes! I knew it had to be simple. The full command should be `echo "ENGINE=MyISAM AUTO_INCREMENT=750 DEFAULT" | sed -E 's/ AUTO_INCREMENT=[0-9]+//'`. Thank you Sundeep! – Todd Hammer Jul 08 '18 at 15:46
  • I should mention that this command works perfectly on both Linux and MacOS with the -E command and not escaping the + symbol. – Todd Hammer Jul 08 '18 at 15:49

1 Answers1

0

Use a BRE instead of an ERE. This will work using any sed in any shell on any UNIX box:

$ echo "ENGINE=MyISAM AUTO_INCREMENT=750 DEFAULT" | sed 's/ AUTO_INCREMENT=[0-9][0-9]*//'
ENGINE=MyISAM DEFAULT
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • Well, since I am dealing with AUTO_INCREMENT the number of digits could be 2 or 10. Appreciate your idea though. – Todd Hammer Jul 09 '18 at 17:23
  • The code I posted will work just fine no matter how many digits it has. Try it. `[0-9][0-9]*` is the BRE equivalent of EREs `[0-9]+` and **every** sed, even the very old default one on Solaris, will support that format. – Ed Morton Jul 09 '18 at 17:37