且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Linux 搜索并替换复杂字符串

更新时间:2023-02-23 08:18:24

当使用 sed 时,你可以使用反向引用 - 用 \N 标记,其中 N 是一个数字.例如.\1 是第一个反向引用.例如.试试这个:

When using sed, you can use back references - marked with \N, where N is a number. E.g. \1 is the first back reference. E.g. try this:

$ echo "\143"|sed 's/\143/abc/'
sed: -e expression #1, char 11: Invalid back reference

这是为了可以匹配已经匹配的内容,例如:

This is to make it possible to match what has been matched already, e.g.:

$ echo 'abcdab'|sed -r 's/(ab)cd\1/x/'
x

其中 (ab)cd\1 表示匹配 ab 作为第一组,然后是 cd,然后是 \1 匹配第一组匹配的任何内容(在本例中为 ab).所以例如这不会匹配:

where (ab)cd\1 means match ab as the first group, then cd and then \1 match whatever the first group matched (ab in this case). So e.g. this won't match:

$ echo 'abcdaa'|sed -r 's/(ab)cd\1/x/'
abcdaa

要使其工作,只需将 \ 转义即可:

To make it work, just escape the \:

$ echo "\143"|sed 's/\\143/abc/'
abc

所以基本上在您的字符串中,只需将所有 \N 替换为 \\N 就可以了.

So basically in your string, just replace all \N to \\N and it should work.

当然,您在实时网站上做任何事情之前都会进行备份,是吗?

Of course, you made a backup before doing anything on a live Web site, yes?