bash sed 意外字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14432562/
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
sed unexpected character
提问by Ahmed Waheed
I'm writing the following script:
我正在编写以下脚本:
v="1,pop";
sed "$v/d" dir/file1
It gives me this error:
它给了我这个错误:
char 3: unexpected `,'
How to solve this? Note: the value of $vcannot be controlled.
如何解决这个问题?注意: 的值$v无法控制。
采纳答案by Suku
You are using sedin a wrong way.
你用sed错了方法。
$ v="1,pop";
$ cat file
1,pop
Suku
JohnGeorge
stackoverflow
serverfault
$ sed '/'$v'/d' file
Suku
JohnGeorge
stackoverflow
serverfault
$ sed "/"$v"/d" file
Suku
JohnGeorge
stackoverflow
serverfault
If you want to substitute a bash variable inside sed, you need to surround it with quotes like I showed above. Also if you want to write modification to file, you need to use sedwith -i.
如果你想在里面替换一个 bash 变量sed,你需要用我上面展示的引号将它括起来。此外,如果您想将修改写入文件,则需要使用sedwith -i。
回答by vara
Using this way in script
在脚本中使用这种方式
v="1,pop"
v="1,流行"
sed "/^$v$/{
sed "/^$v$/{
d
d
}" dir/file1
}"目录/文件1

