简体   繁体   中英

escaping tool for sed

My daily life is hard, i have to patch zillions of files manually, and it's like 80% of a time I'm writing sed regexps for patching files.

so for example I have a line of code:

"    Limit("POST", "*", ".*", 1000, PER_MINUTE),"

which I have inject into some file, in some specific place, so I'm using sed for this:

limits_py=/my/path/limits.py
sed -i "s/\ \ \ \ Limit(\"POST\",\ \"\*\",\ \"\.\*\",\ 10,\ PER_MINUTE),/\ \ \ \ Limit(\"POST\",\ \"\*\",\ \"\.\*\",\ 1000,\ PER_MINUTE),/" ${limits_py}

I'm really tired of writing sed regexps manually for every code string.

So question: how can I generate sed from string?

For example something like this would be a bliss:

echo 'someclass.somemethod("some_argument") # blah blah my comment here' > sed_generator --replace

it's really annoying to "adapt" every line for sed.. is there any tool for this? or even better diff 'ing two files and generate sed regexp replacement for each diff? Maybe it's possible to sed without regexp?

For a start, try:

s|\(["*]\)|\\\1|g

(Notice the list of special characters in the brackets, currently just " and * .)

This will turn

"    Limit("POST", "*", ".*", 1000, PER_MINUTE),"

into

\"    Limit(\"POST\", \"\*\", \".\*\", 1000, PER_MINUTE),\"

Run this on the "before" line and the "after" line, paste the results together with s|...|...| , and you get:

s|\"    Limit(\"POST\", \"\*\", \".\*\", 1000, PER_MINUTE),\"|\"    Limit(\"POST\", \"\*\", \".\*\", 10, PER_MINUTE),\"|

which turns one into the other. Once you get this working (you may need more special characters, like & and $ ), we can work on improvements, like reading the "before" and "after" lines from files. (I'm not sure why you'd want to diff two files and do this for every difference-- why not just overwrite one file with the other?)

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