fedora sed命令替换特殊字符

时间:2022-07-24 16:53:04

i am totally new to sed and as part of script writing i am trying to replace specific string from a fiel. I know the special characters need to be escaped using backslash but the problem is if the special character is first in the line then it is not replaced....

我是sed的新手,作为脚本编写的一部分,我正在尝试替换现场的特定字符串。我知道需要使用反斜杠转义特殊字符,但问题是如果特殊字符是第一行,那么它不会被替换....

For e.g my file contains

例如,我的文件包含

sldgfkls $bdxcv sldflksd

Now if i write the below code

现在,如果我写下面的代码

sed -i 's/\b\$bdxcv\b/abcd/' filename

Then the above word is not replaced....But if the file contains

然后上面的单词不会被替换....但如果文件包含

sldgfkls a$bdxcv sldflksd

Now if i write the below code

现在,如果我写下面的代码

sed -i 's/\ba\$bdxcv\b/abcd/' filename

Then the above word is replaced..... Please Help me here....

然后上面的单词被替换.....请在这里帮助我....

2 个解决方案

#1


0  

Clearly, \b does not consider a dollar sign to be a word character, so there is no word boundary for it to match between space and $.

显然,\ b不会将美元符号视为单词字符,因此在空格和$之间没有字边界。

Perhaps you want this instead:

也许你想要这个:

sed -i 's/\(^\|[\t ]\)\$bdxcv\b/\1abcd/' filename

Assuming yours is GNU sed, see https://www.gnu.org/software/sed/manual/sed.html which contains this definition:

假设您的是GNU sed,请参阅https://www.gnu.org/software/sed/manual/sed.html,其中包含以下定义:

A “word” character is any letter or digit or the underscore character.

“单词”字符是任何字母或数字或下划线字符。

and thus not dollar sign.

因而不是美元符号。

#2


0  

sed cannot operator on strings, only regular expressions. Trying to figure out which characters need to be escaped to disable their regexp (or sed delimiter or sed backreference) meaning to make a regexp in sed behave as if it were a string is a fool's errand, just use a tool that can operate on strings, e.g. awk.

sed不能操作字符串,只能是正则表达式。试图找出哪些字符需要被转义以禁用它们的正则表达式(或sed分隔符或sed反向引用)意味着在sed中制作正则表达式就好像它是一个字符串是一个傻瓜的差事,只需使用一个可以操作字符串的工具,例如AWK。

$ awk '{for (i=1;i<NF;i++) if ($i == "$bdxcv") $i="abcd"} 1' file
sldgfkls abcd sldflks

The above uses string comparison and string assignment - no need to escape anything unless one of the strings contained the string delimiter, ".

以上使用字符串比较和字符串赋值 - 除非其中一个字符串包含字符串分隔符,否则无需转义任何内容。“

#1


0  

Clearly, \b does not consider a dollar sign to be a word character, so there is no word boundary for it to match between space and $.

显然,\ b不会将美元符号视为单词字符,因此在空格和$之间没有字边界。

Perhaps you want this instead:

也许你想要这个:

sed -i 's/\(^\|[\t ]\)\$bdxcv\b/\1abcd/' filename

Assuming yours is GNU sed, see https://www.gnu.org/software/sed/manual/sed.html which contains this definition:

假设您的是GNU sed,请参阅https://www.gnu.org/software/sed/manual/sed.html,其中包含以下定义:

A “word” character is any letter or digit or the underscore character.

“单词”字符是任何字母或数字或下划线字符。

and thus not dollar sign.

因而不是美元符号。

#2


0  

sed cannot operator on strings, only regular expressions. Trying to figure out which characters need to be escaped to disable their regexp (or sed delimiter or sed backreference) meaning to make a regexp in sed behave as if it were a string is a fool's errand, just use a tool that can operate on strings, e.g. awk.

sed不能操作字符串,只能是正则表达式。试图找出哪些字符需要被转义以禁用它们的正则表达式(或sed分隔符或sed反向引用)意味着在sed中制作正则表达式就好像它是一个字符串是一个傻瓜的差事,只需使用一个可以操作字符串的工具,例如AWK。

$ awk '{for (i=1;i<NF;i++) if ($i == "$bdxcv") $i="abcd"} 1' file
sldgfkls abcd sldflks

The above uses string comparison and string assignment - no need to escape anything unless one of the strings contained the string delimiter, ".

以上使用字符串比较和字符串赋值 - 除非其中一个字符串包含字符串分隔符,否则无需转义任何内容。“