shantanuo
shantanuo

Reputation: 32296

removing extra space

cat myfile.sql

DELIMITER $$

USE `AA4`$$

DROP TRIGGER /*!50032 IF EXISTS */ `AT_Card_INSERT_Trigger`$$

CREATE
    /*!50017 DEFINER='root'@'%' */
    TRIGGER `AT_Card_INSERT_Trigger` AFTER INSERT ON `card` 
    FOR EACH ROW BEGIN

The following works as expected and removes the definer clause.

sed -e 's/DEFINER=[^*]*\*/\*/' myfile.sql

But it does not work with spaces after or before equal to sign. For e.g. if I have a line like this...

/*!50017 DEFINER = 'root'@'%' */

Then I need a sed statement something like this...

sed -e 's/DEFINER\ =\ [^*]*\*/\*/' myfile.sql

But there are 2 more possibilites with space (no space before, no space after). It is also possible that there can be more than 1 space before or after "=". How do I handle it all?

Upvotes: 0

Views: 208

Answers (2)

mu is too short
mu is too short

Reputation: 434585

You can just add * (a single space followed by *) to match zero or more spaces:

sed -e 's/DEFINER *= *[^*]*\*/\*/' myfile.sql

If you're on OSX, you could allow for more whitespace than just spaces using this:

# OSX
sed -E -e 's/DEFINER[[:space:]]*=[[:space:]]*[^*]*\*/\*/' myfile.sql

and the same will work with GNU sed(1) if you use -r in place of -E:

# GNU
sed -r -e 's/DEFINER[[:space:]]*=[[:space:]]*[^*]*\*/\*/' myfile.sql

GNU sed(1) also understands \s for whitespace:

# GNU
sed -r -e 's/DEFINER\s*=\s*[^*]*\*/\*/' myfile.sql

Upvotes: 2

Ray Toal
Ray Toal

Reputation: 88378

You already know how to use the Kleene star; it works for spaces too:

sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' myfile.sql

That handles spaces only. If you need tabs as well, drop them into the [ ] sections too. If you have an extended sed that knows about \s you can get even fancier.

Upvotes: 1

Related Questions