sed Case Insensitive Search Matching and Replacement

Originally published at:

Q. How do I perform a case-insensitive search using sed under UNIX / Linux? I’d like to match all combination of word - foo, FOO, FoO and so on while replacing or performing other operations.

sed -ne ‘/pattern/Ip’ filename
This is just for search
-n suppress output, sed will normally print whole file
-e instruction ’ ’ (you can have multiple instructions all followed by -e, also you can omit -e if you have only one isntruction )
/pattern/ - pattern you search for
I - case insensitive (capital i)
p - print, in case you used -n option to suppress output

to replace
sed -e ‘s/find_word/replace_word/Ig’ filename
s/ / / - search
g - globally - without this sed will replace only one match per line, with g option it will replace every match per line
you can experiment with this, also note that if you use -n option to suppress you need to use ‘p’ command to print otherwise you will get blank result