简体   繁体   中英

escaping single quotes in sed

In a file test.py which reads:

#!/usr/bin/env python

import os
import glob

for f in glob.glob('*.txt'):
    print f

...I'd like to programatically replace:

glob.glob('*.txt')

... with:

glob.glob('*.txt')+glob.glob('*.dat') using sed .

I have tried:

sed -i "s,glob.glob('*.txt'),glob.glob('*.txt')+glob.glob('*.dat'),g" test.py

...however this does not replace the string. I have a hunch that this has to do with the use of single quotes in the string to replace and/or the interpretation of the various quotation marks in the shell itself (bash). What is going wrong?

You need to escape all special regex meta characters such as . or * in search pattern.

You can use double quotes in sed command. Also use & in replacement to avoid repeating matched text again.

sed "s/glob\.glob('\*\.txt')/&+glob.glob('*.dat')/" test.py

#!/usr/bin/env python

import os
import glob

for f in glob.glob('*.txt')+glob.glob('*.dat'):
    print f

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM