用sed替换IP地址的结果为:sed:-e表达式#1,char 57:不匹配(或\(


0
sed -e 's/\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\.)\(.*\)/ANONYMOUS\2/p' $IPLOG

我正在尝试将IP地址从$ IPLOG替换为ANONYMOUS,但出现此错误

sed: -e expression #1, char 57: Unmatched ( or \(

Answers:


2

您在)s 之前缺少了一个反斜杠。您可能的意思是g(全局替换;每次出现)而不是p(如果替换成功,则打印,这将导致替换的行被打印两次)。和\{1,\}(或\+在某些实现中)而不是*一个或多个而不是零或多个):

sed 's/[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}/ANONYMOUS/g' < "$IPLOG"

或分解一下:

sed 's/\([0-9]\{1,\}\.\)\{3\}[0-9]\{1,\}/ANONYMOUS/g' < "$IPLOG"

一些sed实现-E(或-r类似ssed的GNU或更旧版本sed)具有扩展的正则表达式,可以使它更加清晰易懂:

sed -E 's/([0-9]+\.){3}[0-9]+/ANONYMOUS/g' < "$IPLOG"

太好了,非常感谢。现在正在工作。
爵士乐Ing
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.