0

I am trying to do a sed replacement with pattern

s/^\(ENGINE.*\)DEFAULT CHARSET=utf8mb4$/\1/g

but it doesn't seem to work. Can someone please tell me what am I doing wrong?

I am trying to replace

ENGINE=InnoDB AUTO_INCREMENT=19391 DEFAULT CHARSET=utf8mb4

with

ENGINE=InnoDB AUTO_INCREMENT=19391
Cyrus
  • 84,225
  • 14
  • 89
  • 153
Pragyan
  • 146
  • 3
  • 17
  • 2
    It seems to work for me. What does your call to `sed` look like? – dee-see May 12 '18 at 11:39
  • Probably caused by some leading or trailing spaces. Remove those anchors. – revo May 12 '18 at 11:47
  • @dee-see The lines that I want to replace in the file look like: `) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;` or `) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;` I just want to remove 1`DEFAULT CHARSET=utf8mb4`. It doesn't work for me. My sed is `$ sed 's/utf8/utf8mb4/g; s/^\(ENGINE.*\)DEFAULT CHARSET=utf8mb4$/\1/g;' `. Thanks :) – Pragyan May 12 '18 at 14:38

2 Answers2

0

EDIT: As per OP's request adding 1 more solution too now.

sed 's/\().*ENGINE[^ ]*\)\( DEFAULT.*\)/\1/'  Input_file

Following sed may help you on same.

sed 's/ DEFAULT.*//'  Input_file

In case you want to save the output into Input_file itself then use sed -i option in above code. Also in case of taking backup of original Input_file and saving output into Input_file then use sed -i.bak for same.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • Actually, `DEFAULT CHARSET=utf8mb4;` can appear anywhere else in the file. Thanks for this, but its not specific enough :) – Pragyan May 12 '18 at 17:30
  • @noctni8, than please post more good examples of your Input_file and let me know will fix it in my code then. – RavinderSingh13 May 12 '18 at 17:31
  • My apologies, I couldn't share enough. In the file I have `) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;` or `) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;` I just want to remove `DEFAULT CHARSET=utf8mb4` I am almost there, just can't figure out how to escape `) ` as `\)` is group closing in sed. Any suggestions? – Pragyan May 12 '18 at 17:36
  • @noctni8, sorry still not clear do you only want to remove `DEFAULT...utf`? or with that you want to remove `(` and `)` too? Please be clear. – RavinderSingh13 May 12 '18 at 17:41
  • Now I am using `s/^\(.*ENGINE.*\) DEFAULT CHARSET=utf8mb4;$/\1;/g` to achieve this. If you observe, I am using `.*` before `ENGINE`. I can't figure out how to match `) `. Please help – Pragyan May 12 '18 at 17:45
  • @noctni8, I am still not sure why you are not using simple `sed /s/ DEFAULT.*//' which I have given it will simply remove everything from DEFAULT to till last of line? – RavinderSingh13 May 12 '18 at 17:48
  • I am not using that coz in the file there are other `DEFAULT CHARSET=utf8mb4` which I don't want to remove. The code you have suggested will remove every occurrence of `DEFAULT...` which is something I don't want. I need to be more specific with my regex. – Pragyan May 12 '18 at 17:52
  • @noctni8, please try my EDIT code and let me know if that helps you? – RavinderSingh13 May 12 '18 at 18:04
  • DOWN VOTER, please do mention the reason for down vote here??(Addressed to down voter not to OP) – RavinderSingh13 May 13 '18 at 03:33
0

The $ at the end of your regexp is telling sed to only match the regexp if it occurs at the end of the line. It therefore doesn't match since the line ends with ;. So just don't do that, do this instead:

s/^\(ENGINE.*\)DEFAULT CHARSET=utf8mb4/\1/g
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • In that case I think, `s/^\(ENGINE.*\)DEFAULT CHARSET=utf8mb4;$/\1/g` should work, isn't it? There is a `) ` before `ENGINE` and I have no idea how to escape `)` as `\)` is group closing in sed. Any suggestions? – Pragyan May 12 '18 at 17:28
  • You don't need to escape `)`, there's nothing special about that char in a BRE like sed uses by default. You can add the `;` if you want it to be art of your regexp but that's not whay you had in your question. – Ed Morton May 12 '18 at 20:57