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)

1 Answer 1


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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .