Making SED case insensitive

Dears,

In the below string, please let me know how to make the sed search case-incensitive. I have more such lines in my script instead of [Aa] let me know any other easier option.
sed -n '/dn: MSISDN=/,/^\s*$/p' full.ldif > temp ; sed -n '/,dc=msisdn,ou=identities,'"$LDAP_ROOT_DN"'/,/^\s*$/p' temp > alias_MSISDN_PL.ldif ; rm temp

regards,
Kamesh G

If you're using GNU sed, you can add the I modifier to the end.

e.g.

$ echo HELLO | sed "s/hello/hi/I"
hi

Otherwise, you can pipe the input through tr first, for example.

The I modifier also works with addresses:

echo HELLO | sed "/hello/I s//hi/"
sed -n '/dn: MSISDN=/I,/^\s*$/p'
1 Like

I can't offer you a (single) command but a procedure how to do it - even with a standards-conforming (non-GNU) sed :

  • First, save your pattern space to the hold space.

  • Next, you use the y/.../.../ -command to transliterate to lower

  • Finally you do your matching and get from the hold space the saved copy of the pattern space on which you perform whatever action you wanted to perform.

Here is an (very simple) example: we search (case-insensitively) for "hello" at the beginning of the line and add a equal-sign in front of it. The comments are NOT part of the script:

echo 'Hello
foo
hElLo
helLO' |\
sed 'h                 # pattern to hold space
      y/HELO/helo/     # we need only to convert what we match
      /^hello/ {
            g          # get the original line from hold space
            s/^/=/
            b end
      }
      /^hello/ ! {
            g
      }
      :end'

I hope this helps.

bakunin

2 Likes

Good idea!
Can be optimized: after the b end one is automatically in an "else branch" and does not need to repeat and negate the previous condition.

echo 'Hello
foo
hElLo
helLO' | sed '
  h
  y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/
  /^hello/{
    g
    s/^/=/
    b end
 }
 g
 :end
'

This time I have done a complete ASCII lowercase conversion.
This works with all sed versions.
Of course the GNU I modifier is much simpler.