我知道这已经被问过,但我正努力确定错误的根本原因与我的脚本。 我经历了其他问题,并试图转换(切换/ \为@),但它只是不起作用,我仍然得到相同的错误?
只要我在最后4(从底部)joinexpression式,我开始得到错误,..
当我运行这个
clustername="'XXXCluster'" seed=xx.xxx.xxx.xx ip=xx.xxx.xxx.xx hint="/opt/cassandra/data/hints" data="/opt/cassandra/data/data" commitlog="/opt/cassandra/data/commitlog" cache="/opt/cassandra/data/saved_caches" sed -i -e "s/\(cluster_name:\).*/\1$clustername/" \ -e "s/\(- seeds:\).*/\1$seed/" \ -e "s/\(listen_address:\).*/\1$ip/" \ -e "s/\(rpc_address:\).*/\1$ip/" \ -e "s/\(broadcast_rpc_address:\).*/\1$ip/" \ -e "s/\(hints_directory:\).*/\1$hint/" \ -e "s/\(data_file_directory:\).*/\1$data/" \ -e "s/\(commitlog_directory:\).*/\1$commitlog/" \ -e "s/\(saved_caches_directory:\).*/\1$cache/" /opt/cassandra.yaml
我得到这个sed:-eexpression式#6,字符29:未知选项's'
但我不能看到如何解决这个问题,有人可以帮我吗?
提前致谢..
在你的sed中使用%而不是/。
sed -e 's%search_string%replace_with%'
我想你的问题线有斜线,sed会和它合作。
编辑:
由于您正在使用replacestring的variables,所以您的斜线很重要。
我的第一个答案是有点missleading。 对不起。
例:
我们有一个文件nada.txt,内容是“test:”/ a / place / in / universe“'
$ cat nada.txt test: "/a/place/in/universe"
带有replace目录的variables
$ dir="/new/place/in/haven" $ echo $dir /new/place/in/haven
让我们尝试失败
$ sed -e "s/\(test: \).*/\1$dir/" nada.txt sed: -e expression #1, char 19: unknown option to `s'
再一次,这次用%(“///”到“s %%%”)replace了斜杠
$ sed -e "s%\(test: \).*%\1$dir%" nada.txt test: /new/place/in/haven
要么
$ sed -e 's%\(test: \).*%\1'$dir'%' nada.txt test: /new/place/in/haven
看单引号,你需要四个取出variables。 它看起来像这样:%%'$ dir'%'因为在shell上下文中,单引号不能parsingvariables:
$ echo 'Such a text and $dir' Such a text and $dir
双引号就像预期的那样工作。
$ echo "Such a text and $dir" Such a text and /new/place/in/haven
希望有所帮助