将stdin和管道打到sed似乎不起作用

KB *_*Kim 4 grep replace

我正在尝试过滤和替换标准输入,以便仅捕获针对我的风格调整的特定字符串.例如,假设标准输入字符串是

"KB: the system ran into a critical error" 
Run Code Online (Sandbox Code Playgroud)

然后我将grep这一行,将"KB:"改为"###",然后输出到STDOUT这样

"### the system ran into a critical error"
Run Code Online (Sandbox Code Playgroud)

我试过Fedora 12(实际上还有很多其他的)

$ grep "KB:" - | sed -e 's/KB:/###/g'
KB: the system ran into a critical error <--- This is what I typed in manually.
                                         <--- Nothing is printed
Run Code Online (Sandbox Code Playgroud)

我创建了一个文本文件text.txt,它包含相同的示例字符串,然后grep从文件中读取如下:

$ grep "KB:" text.txt | sed -e 's/KB:/###/g'
### the system ran into a critical error <--- Correctly displayed
$
Run Code Online (Sandbox Code Playgroud)

grep/sedSTDIN方法不起作用的内部和为什么会有什么不同?xargs我不想听到使用等替代品,而是想知道为什么我的尝试会失败.

Ste*_*eve 5

这对我有用:

echo "KB: the system ran into a critical error" | grep "KB:" | sed -e 's/KB:/###/g'

请注意命令中的-字符排除grep.

grep在这里使用是没用的.您可以将代码缩小到大小:

echo "KB: the system ran into a critical error" | sed -e 's/KB:/###/g'
### the system ran into a critical error