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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:18:40  来源:igfitidea点击:

sed unexpected character

bashsed

提问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

回答by alex

You need the hashbangso your shell knows how to execute the script.

您需要hashbang以便您的 shell 知道如何执行脚本。

#!/bin/sh

Then you'll get...

然后你会得到...

sed: 1: "1,pop/d": expected context address

sed: 1: "1,pop/d": 预期的上下文地址

...which tells your the commands were executed. :)

...它告诉您命令已执行。:)