0

I use the following command to replace strings in texts:

    grep -rl "abc" A.txt | xargs sed -i 's/abc/efg/g'.

But I also want to replace Abc with efg. The following command seems not working.

    grep -irl "abc" A.txt | xargs sed -i 's/abc/efg/g'

using the -i for grep is to find case-insensitive lines, but in the subsequent sed command, it will won't admit Abc.

So, my question is how to sed to replace case-insensitive texts?

mining
  • 3,557
  • 5
  • 39
  • 66

2 Answers2

0
awk 'BEGIN{IGNORECASE=1} {gsub(/abc/,"efg")}1' A.txt > a.tmp && mv a.tmp A.txt

Example:

 echo -e "abc\nABC\nhey abc" |awk 'BEGIN{IGNORECASE=1} {gsub(/abc/,"efg")}1'
efg
efg
hey efg
P....
  • 17,421
  • 2
  • 32
  • 52
  • Hi, thank you for answer! In my case, there are many strings to be replaced. I think this method might generate many temporary files. For example, I need to execute a lot of commands like this: `find . -name "*.tex" | xargs grep -irl "tu2015futher" | xargs sed -i 's/tu2015futher/tu\_futher\_2015/g'`. And there are many `tu2015futher`-like strings to be replaces to other corresponding strings. – mining Sep 08 '16 at 12:42
0

Capitalize the 'I'.

grep -irl "abc" do.py | xargs sed -i 's/abc/efg/gI'

Or, use another regex like this

grep -irl "abc" do.py | xargs sed -i 's/[Aa]bc/efg/g'
luoluo
  • 5,353
  • 3
  • 30
  • 41
  • Hi, thank you for answer! In the post, I use `abc` and `efg` for example. In my case, there are many other strings, thus, this method might be not efficient. – mining Sep 08 '16 at 12:37