shell sed替换文本文件中的特殊字符但无法替换它

时间:2022-05-13 16:51:20

I am trying to replace sam_18 with sam in a text file but it doesnt produce the correct result.

我试图用文本文件中的sam替换sam_18,但它不会产生正确的结果。

sed -i -e 's/sam_18/sam/g'

It doesnt produce any output. I am a bit confused if it is considering '_' in between as a special character. Any help?Thanks

它不会产生任何输出。如果它将'_'视为特殊字符,我有点困惑。有什么帮助吗?谢谢

1 个解决方案

#1


Option -i requires a file (at least) to perform the in-place substitution:

选项-i需要一个文件(至少)来执行就地替换:

sed -i -e 's/sam_18/sam/g' myfile

If no file are provided, sed reads from the standard input (or an input pipe). You cannot use -i in that case. You would then do something like:

如果没有提供文件,则sed从标准输入(或输入管道)读取。在这种情况下你不能使用-i。然后你会做类似的事情:

cat myfile | sed -e 's/sam_18/sam/g' > newfile

#1


Option -i requires a file (at least) to perform the in-place substitution:

选项-i需要一个文件(至少)来执行就地替换:

sed -i -e 's/sam_18/sam/g' myfile

If no file are provided, sed reads from the standard input (or an input pipe). You cannot use -i in that case. You would then do something like:

如果没有提供文件,则sed从标准输入(或输入管道)读取。在这种情况下你不能使用-i。然后你会做类似的事情:

cat myfile | sed -e 's/sam_18/sam/g' > newfile