如何使 sed 从标准输入读取?

我在努力

grep searchterm myfile.csv | sed 's/replaceme/withthis/g'

还有

unknown option to `s'

我做错了什么?

编辑:

根据注释,代码实际上是正确的

grep searchterm myfile.csv | sed 's/replaceme/withthis/g'
# my comment

出于某种原因,我的注释被输入到 sed 中,非常奇怪。

142793 次浏览

使用—— expression 选项

grep searchterm myfile.csv | sed --expression='s/replaceme/withthis/g'
  1. 使用 vi myfile.csv打开文件
  2. Escape
  3. 类型 :%s/replaceme/withthis/
  4. 输入 :wq,然后按 Enter

现在您的文件中将有新的模式。

使用“-e”指定 sed 表达式

cat input.txt | sed -e 's/foo/bar/g'

要使 sed从 stdin 捕获,而不是从文件捕获,应该使用 -e

像这样:

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 $4}'

如果您正在尝试对文件中的文本进行就地更新,那么在我看来这更容易理解。

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