2

How to specify a sed regexp address which is case-insensitive?

E.g.,

sed '/my-kw/d'

But I want sed to remove lines with my-kw in any cases.

Thanks

1
  • Should this question be migrated to the Unix and Linux StackExchange community? Jan 24, 2016 at 7:09

3 Answers 3

2
sed '/my-kw/Id'

This will perform the match case-insensitive.

The switch is in uppercase itself, to avoid confusion with the i command that is offered by sed to insert a line into the stream.

4
  • Perfect thanks. Hmm, hope my following multi-line comment can go through $ jot -c 5 'A' | tee /dev/tty | sed '/c/Id' A B C D E A B D E
    – xpt
    Jun 30, 2013 at 13:28
  • No, my prevous multi-line comment didn't go through with nice formatting, but you can see that C was removed from the list. You can run the sample your self jot -c 5 'A' | tee /dev/tty | sed '/c/Id', where the jot is from the athena-jot package under Debian/Ubuntu.
    – xpt
    Jun 30, 2013 at 13:31
  • 1
    Note that I is a GNU sed extension. Sep 22, 2015 at 15:16
  • 3
    This is not portable. See: stackoverflow.com/questions/4412945/… Jan 24, 2016 at 7:08
5

Just use the I switch:

$ echo fooFOO | sed 's/o/a/Id'
faaFaa

From the sed FAQ:

GNU sed 3.02 and ssed also offer the /I switch for doing a case-insensitive match. For example,

echo ONE TWO | sed "s/one/unos/I"      # prints "unos TWO"
4
  • and the ones who upvote the answer, my question is for sed address. See my comment on other thread.
    – xpt
    Jun 30, 2013 at 13:25
  • I am working with n - quiet and p - print flag to print part of a line: $(dhclient $eth|sed -nr 's/rtnetlink answers: (.*)/\1/Ip'). But the caseinsensitive does not work. Actual behaviour RTNETLINK answers: Operation not permitted, expected Operation not permitted. Same $(dhclient $eth|sed -r 's/rtnetlink answers: (.*)/\1/I')
    – Timo
    Sep 25, 2021 at 18:00
  • 1
    @Timo please ask a new question where you can show exact input and desired output and tell us what version/implementation of sed you have. The match is working, otherwise you would have no output, so it will be something about your input.
    – terdon
    Sep 25, 2021 at 18:09
  • You are right, nothing to do with case problem, but with input. I posted a new one
    – Timo
    Sep 25, 2021 at 19:11
1

This might work for you:

sed '/[mM][yY]-[kK][wW]/d' file or:

sed 'h;y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/;/MY-KW/d;x' file

You must log in to answer this question.

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