uberrebu
uberrebu

Reputation: 4339

how to use sed to replace string containing parenthesis

I am trying to use sed to replace the following but not working

replace datetime.now(pytz.utc) with datetime.utcnow() recursively

i have tried the following

grep -rl "datetime.now(pytz.utc)" . | xargs sed -i 's/datetime.now\(pytz.utc\)/datetime.utcnow\(\)/g'

mac command equivalent

LC_ALL=C

grep -e "datetime.now(pytz.utc)" -rl . | xargs sed -i '' 's/datetime.now\(pytz.utc\)/datetime.utcnow\(\)/g'

as you can see i tried to escape all the parentheses but does not work

anyone know how to properly use sed to replace datetime.now(pytz.utc) with datetime.utcnow()?

Upvotes: 0

Views: 740

Answers (1)

jared_mamrot
jared_mamrot

Reputation: 26505

I tried to explain in the comments, but obviously I wasn't clear. Here are two potential solutions to your problem:

Using your 'grep/xargs' method:

grep -rl "datetime.now(pytz.utc)" . | xargs sed -i 's/datetime.now(pytz.utc)/datetime.utcnow()/g'

Using the 'find/exec' method:

find . -type f -exec sed -i 's/datetime.now(pytz.utc)/datetime.utcnow()/g' {} \;

Both options will replace "datetime.now(pytz.utc)" with "datetime.utcnow()" in the files found. Both answers are platform independent provided you have GNU sed, not BSD sed.

Upvotes: 3

Related Questions