我正在尝试
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,非常奇怪。
使用 --expression 选项
grep searchterm myfile.csv | sed --expression='s/replaceme/withthis/g'
使用“-e”来指定sed表达式
cat input.txt | sed -e 's/foo/bar/g'
sed
将从stdin读取。 - umläute-e
有必要吗?没有加-e
也能工作。我想知道使用-e
的目的是什么?谢谢。 - Timecho foo | sed 's/foo/bar/g'
可以在没有 -e
的情况下工作,输出 bar
。所以我一定错过了他的问题。 - Timsed
从标准输入中捕获,而不是从文件中捕获,您应该使用-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
vi myfile.csv
命令打开文件:%s/replaceme/withthis/
:wq
并按下 Enter 键现在您的文件中将会有新的模式。
replaceme
或withthis
中有一个/
。尝试使用:'s#replaceme#withthis#g'
。 - cababunga