简体   繁体   English

sed用文件夹中所有文件中包含破折号的字符串替换字符串

[英]sed replace string with string containing dash in all files in folder

In all files in a fodler, I am trying to replace all strings of the type: 在抚摸者的所有文件中,我试图替换所有类型的字符串:

load(alpha)

to

load(alpha, lib="lib=~/project-a/alpha")

using sed I tried: 我尝试使用sed:

find . -type f -print0 | xargs -0 sed -i 's/load(alpha)/load(alpha, lib="lib=~/project-a/alpha")/g'

But I get the error: sed: -e expression #1, char 39: unknown option to `s' 但是我得到了错误:sed:-e表达式#1,字符39:`s'的未知选项

Any idea what I am missing? 知道我缺少什么吗? Thank you! 谢谢!

You get that error because the / delimiters with the substitute command are conflicting with the directory separators in your replacement; 之所以会出现此错误,是因为带/分隔符的replace命令与替换中的目录分隔符冲突; after the first directory separator is encountered, sed considers the replacement aborted. 遇到第一个目录分隔符后,sed认为替换已中止。 Change your delimiter to something else for a more clearer command. 将定界符更改为其他内容,以获得更清晰的命令。

sed -i 's!load(alpha)!load(alpha, lib="lib=~/project-a/alpha")!g'
find . -type f -print0 | xargs -0 sed -i 's/load(alpha)/load(alpha, lib="lib=~\/project-a\/alpha")/g'

In your command the character / is tehe delimiter, so you need to escape any forward-slashes that are part of your data as in /project-a/alpha -> \\/project-a\\/alpha 在命令中,字符/是定界符,因此您需要转义属于数据的任何正斜杠,如/project-a/alpha > \\/project-a\\/alpha

Alternatively you can a different delimiter (in this case the # character) 另外,您可以使用其他定界符(在本例中为#字符)

find . -type f -print0 | xargs -0 sed -i 's#load(alpha)#load(alpha, lib="lib=~/project-a/alpha")#g'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM