revise "([^/]+)$" into '([^/]+)$' in a lot of files under a directory.

Pieter van Oostrum pieter-l at vanoostrum.org
Sun Nov 17 12:39:56 EST 2019


Hongyi Zhao <hongyi.zhao at gmail.com> writes:

> Hi,
>
> I have a lot of files which resides under a directory, and I want to 
> resive all of the content  "([^/]+)$" in these files into '([^/]+)$'.
>
> I have tried with sed/xargs, but it seems so difficult for dealing with 
> the \ ,   is there any convinent method to to this job with python? 
>
> Regards

It isn't that difficult with sed, only you have to chose a different character than / in the substitute command, one that is not present in both texts, e.g instead of s/a/b/ use s=a=b=.

And then the special characters " ' () [ and $ must be escaped for the shell, and [ and $ also for the regexp.
Then it comes down to
sed -e s=\"\(\\[^/]+\)\\$\"=\'\(\[^/]+\)\$\'= file
-- 
Pieter van Oostrum
WWW: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]


More information about the Python-list mailing list