Linux 如何让 sed 从标准输入读取?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9984748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get sed to read from standard input?
提问by deltanovember
I am trying
我在尝试
grep searchterm myfile.csv | sed 's/replaceme/withthis/g'
and getting
并得到
unknown option to `s'
What am I doing wrong?
我究竟做错了什么?
Edit:
编辑:
As per the comments the code is actually correct. My full code resembled something like the following
根据评论,代码实际上是正确的。我的完整代码类似于以下内容
grep searchterm myfile.csv | sed 's/replaceme/withthis/g'
# my comment
And it appears that for some reason my comment was being fed as input into sed. Very strange.
似乎出于某种原因,我的评论被作为输入输入到 sed 中。很奇怪。
采纳答案by Ben Davis
use the --expression option
使用 --expression 选项
grep searchterm myfile.csv | sed --expression='s/replaceme/withthis/g'
回答by Teja
- Open the file using
vi myfile.csv
- Press Escape
- Type
:%s/replaceme/withthis/
- Type
:wq
and press Enter
- 使用打开文件
vi myfile.csv
- 按 Escape
- 类型
:%s/replaceme/withthis/
- 输入
:wq
并按下Enter
Now you will have the new pattern in your file.
现在,您的文件中将拥有新模式。
回答by uml?ute
use "-e" to specify the sed-expression
使用 "-e" 指定 sed 表达式
cat input.txt | sed -e 's/foo/bar/g'
回答by LeoChu
To make sed
catch from stdin , instead of from a file, you should use -e
.
要从sed
stdin 捕获而不是从文件中捕获,您应该使用-e
.
Like this:
像这样:
curl -k -u admin:admin https://$HOSTNAME:9070/api/tm/3.8/status/$HOSTNAME/statistics/traffic_ips/trafc_ip/ | sed -e 's/["{}]//g' |sed -e 's/[]]//g' |sed -e 's/[\[]//g' |awk 'BEGIN{FS=":"} {print }'
回答by Wes
If you are trying to do an in-place update of text within a file, this is much easier to reason about in my mind.
如果您尝试对文件中的文本进行就地更新,在我看来这更容易推理。
grep -Rl text_to_find directory_to_search 2>/dev/null | while read line; do sed -i 's/text_to_find/replacement_text/g' $line; done