bash sed 替换包含冒号的文件中的行号

Ber*_*ghy 1 bash sed

我使用的是 RHEL 7.3。第 12 行myfile.txt看起来像:

图像:/currentimage/myimage

我需要一个 bash 脚本将其更改为:

图像:/newimage/otherimage

我尝试这样做:

sed -i '12s/image: /currentimage/myimage/image: /newimage/otherimage/' ./myfile
Run Code Online (Sandbox Code Playgroud)

但它失败了:

sed: unknown option to `s'
Run Code Online (Sandbox Code Playgroud)

小智 6

您正在使用/作为sed分隔符,并且它在您的路径中使用。尝试使用|作为分隔符。

sed -i '12s|image: /currentimage/myimage|image: /newimage/otherimage|' ./myfile
Run Code Online (Sandbox Code Playgroud)

此外,您可以/像这样转义文件路径中的每个文件\/