本文介绍了如何使用SED查找并用“ /”替换URL字符串。目标字符串中的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图通过OS X Terminal使用SED来执行查找和替换。

I'm attempting to use SED through OS X Terminal to perform a find and replace.

想象一下,我在整个文本文件中乱扔了这个字符串:

Imagine I have this string littered throughout the text file: http://www.find.com/page

我想用以下字符串替换它:

And I want to replace it with this string: http://www.replace.com/page

我遇到了麻烦,因为我不确定如何正确地转义或在字符串中使用 /字符。例如,如果我只是想找到 cat并替换为 dog,我发现以下命令非常有效:

I'm having trouble because I'm not sure how to properly escape or use the "/" character in my strings. For example if I simply wanted to find "cat" and replace with "dog" I've found the following command that works perfectly:

sed -i '' 's/cat/dog/g' file.txt

有人吗对如何仅实现相同功能有什么想法,而不是让cat and dog具有包含 /字符的字符串或URL?我尝试了多种转义 /字符的方法,但似乎SED无法再查找该字符串并且它不执行任何查找和查找。

Does anyone have any ideas on how to achieve the same functionality only instead of cat and dog have strings or URLs that container the "/" character? I tried many different ways of escaping the "/" characters but then it seems as if SED can no longer "find" the string and it doesn't perform any find & replace actions.

非常感谢任何帮助或提示。

Any help or tips are greatly appreciated.

谢谢!

推荐答案

/ 不是中的 分隔符sed 命令,它只是可能的命令之一。对于此示例,例如,可以使用代替,因为它与字符串不冲突;

/ is not the delimiter in sed commands, it's just one of the possible ones. For this example, you can for example use , instead since it does not conflict with your strings;

echo 'I think http://www.find.com/page is my favorite' |
    sed 's,http://www.find.com/page,http://www.replace.com/page,g'

这篇关于如何使用SED查找并用“ /”替换URL字符串。目标字符串中的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 06:56