Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When running the following sed command under Windows (to uppercase a keyword which isn't part of another word):

"c:\Program Files (x86)\GnuWin32\bin\sed.exe" -e "s/[^a-z]*declare[^a-z]*/DECLARE/I" "SqlFile.sql"

will turn declare @returntable from SqlFile.Sql into DECLAREreturntable.

But adding capturing groups to transport the surrounding characters to the destination string, will leave declare lowercased:

"c:\Program Files (x86)\GnuWin32\bin\sed.exe" -e "s/([^a-z]*)declare([^a-z]*)/\\1DECLARE\\2/I" "SqlFile.sql"

Why does sed apply different rules to grouped sets in comparison to ungrouped ones?

(I know that the \b word boundaries are a better way to uppercase keywords, but I observed this sed behaviour and was wondering what the reason was)

share|improve this question
up vote 1 down vote accepted

It is because "(foo)" is not a capturing group for sed, only "\(foo\)" is.

Sed would normally error out that you use \1 and \2 without any capturing groups defined, but it doesn't because "\\1" apparently doesn't expand to \1 in your shell (command.exe?) like they do in bash.

So you're trying to replace a string with literal parentheses with one with literal backslashes and it does not match, keeping declare lowercase.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.