1

I am using this command to find and replace strings in files.

find . -name "dog.*" -exec sed -i "s/dog/cat/Ig" {} +

It works fine for exact case match, but I want it to be case insensitive. I used I but it doesn't seem to be working. How can I fix this? I simply want to be able to replace the strings using a simple one-liner. The name of the files are dog with different extensions and I want to replace all incidents of dog with cat regardless of case.

Update: I have tried both lowercase and uppercase I but still not working. I have also tried it with gsed.

I have just realized something that I wonder may be the root cause of this issue. The case where it is not doing the replacement is when my keyword is part of a string. It will replace all instances of dog but if there is something like dog_food, it will not do the replacement.

  • 1
    lowercase `i`, not uppercase `I`. – Barmar Aug 23 '19 at 23:19
  • 1
    [Case-insensitive search and replace with sed](https://stackoverflow.com/q/4412945/608639), [Case insensitive search matching with sed?](https://stackoverflow.com/q/39178188/608639), [Using sed to delete a case insensitive matched line](https://stackoverflow.com/q/2157288/608639), etc. – jww Aug 24 '19 at 05:18
  • @Barmar `GNU sed` allows both `i` and `I` for substitution command and only `I` for filtering, for example: `printf 'Sea\nset\nsat\n' | sed -n '/se/Ip'` as `i` in this usecase would mean `insert` command and not case-insensitive – Sundeep Aug 24 '19 at 07:19
  • @sfr do you get different results for `echo 'dog Dog DoG' | sed 's/dog/cat/Ig'` and `echo 'dog Dog DoG' | sed 's/dog/cat/ig'` ? you have tagged it `linux` so I'm assuming you have `GNU sed` in which case you should get same output for these two commands – Sundeep Aug 24 '19 at 07:31
  • @Barmar That does not work either. I tried it with gsed as well, but no luck. – programminglearner Aug 24 '19 at 18:14
  • @Sundeep I did get the same results. – programminglearner Aug 24 '19 at 18:15
  • I've just updated the post with more details. – programminglearner Aug 24 '19 at 18:22
  • 1
    I can't reproduce that problem with partial words. I just tried `echo 'Dog_food dog' | sed 's/dog/cat/ig'` using GNU sed 4.2.1 on Linux, the output was `cat_food cat` – Barmar Aug 24 '19 at 20:12
  • I can't reproduce that problem. It seems to work fine (without regard to the case of the expressions). – zx485 Aug 24 '19 at 20:41
  • After going through the files manually, I realized there have been some typos by the people who published the files which is causing the issue. Overall, it now works fine. – programminglearner Aug 25 '19 at 08:33

0 Answers0