Search code examples
macossedhomebrewgnu

Is there a relative case specifier for gsed?


The case insensitive substitution signifier in a sed command appears itself to be case insensitive. Appending i/I to the sed statement seems to give the same result.

echo a | gsed ’s_A_Z_i’

returns: Z

echo a | gsed ’s_A_Z_I’

also returns: Z

echo A | gsed ’s_A_z_I’

returns: z

echo A | gsed ’s_a_Z_i’

returns: Z

gsed returns the case-specific substitute text.

Is there a compact way to specify a case insensitive search with replacement in the case matching that of the find? (replace a with z and A with Z)

Of course, the problem could be solved with two commands but I'm looking for something more compact.

Secondarily, is there a difference in the effect of the i/I suffix? (Not the -i flag)

This follows from, but differs from [a previous question](How to use GNU sed on Mac OS X)! on the use of gnu-sed in OSX, after standard homebrew installation.

I do not have a specific application in mind. I'm learning some of the intricacies of gnu-sed on OSX and would like to know how it handles case.


Solution

  • The i/I option to the s command modifies only the PATTERN and does not affect the REPLACEMENT. Please imagine the following usage:

    str='Login'
    echo "$str" | sed 's/login/Login/i'
    

    This example attempts to normalize the input into the upper camel case to accept both login and Login. If the i/I option affects the REPLACEMENT, we cannot obtain the expected result.

    If you want to map a to z and A to Z, try instead:

    sed y/Aa/Zz/