bash 在 sed 命令中使用变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11146098/
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-09 22:18:15  来源:igfitidea点击:

Use a variable in a sed command

bashvariablessedescaping

提问by user788171

I can't seem to use a variable in a sed command, for example:

我似乎无法在 sed 命令中使用变量,例如:

sed "24s/.*/"$ct_tname"/" file1.sas > file2.sas

I want $ct_tnamethe variable, not literally $ct_tname, which is what I keep getting.

我想要$ct_tname变量,而不是字面意思$ct_tname,这就是我不断得到的。

Anybody know how to get this to work?

有谁知道如何让这个工作?

The problem is actually more complex and I omitted some information.

问题其实比较复杂,我省略了一些信息。

ct_fname="%let outputfile="/user/ct_"".csv";"

Here, $1is the argument passed in at the start of my bash script (sed is being run inside a bash script).

$1是在我的 bash 脚本开始时传入的参数(sed 在 bash 脚本中运行)。

This doesn't run successfully, but it does run if I replace ct_fnamewith

这没有成功运行,但如果我替换ct_fname为它确实运行

ct_fname="%let table=ct_;"

Is there a way to get the first ct_fnameto be passed successfully?

有没有办法让第一个ct_fname顺利通过?

回答by c00kiemon5ter

you need to use double quotes (") instead of single quotes ('). single quotes pass their content literally, without translating variables (expansion).

您需要使用双引号 ( ") 而不是单引号 ( ')。单引号字面传递它们的内容,而不翻译变量(扩展)。

try

尝试

sed "24s/.*/\"$ct_tname\"/" file1.sas > file2.sas

btw, if you're going to be editing a file (that is if file2.sasis a temporary file), you should be using edinstead.

顺便说一句,如果你要编辑一个文件(也就是说,如果file2.sas是一个临时文件),你应该使用它ed

回答by Garik

In my case, i just remplaced single quotes by the double ones:

就我而言,我只是用双引号替换了单引号:

for a in $(cat ext.cnf); do sed -n "/$a$/p" file1 >> file2; done

For now, it's working well...

目前,它运行良好......

回答by Roger Dueck

The problem is that when $ct_fnameis substituted, sedsees extra /separators, so

问题是当$ct_fname被替换时,会sed看到额外的/分隔符,所以

sed "24s/.*/"$ct_tname"/" file1.sas > file2.sas

becomes

变成

sed "24s/.*/"%let outputfile=/user/ct_ARGUMENT1.csv;"/" file1.sas > file2.sas

and you'll get a sederror because there are 5 /instead of the expected 3.

你会得到一个sed错误,因为有 5 个/而不是预期的 3 个。

Instead, change your sedseparators to an unused character like |or :, and either single or double quotes will work just fine:

相反,将sed分隔符更改为未使用的字符,例如|:,单引号或双引号都可以正常工作:

sed '24s|.*|'$ct_tname'|' file1.sas > file2.sas
sed "24s|.*|"$ct_tname"|" file1.sas > file2.sas

回答by Kenneth Hoste

You need to use double (") quotes, with single (') quotes the value of the variable doesn't get replaced. Since you have double quotes in your replacement text, you need to escape them:

您需要使用双 ( ") 引号,使用单 ( ') 引号不会替换变量的值。由于您的替换文本中有双引号,因此您需要对它们进行转义:

sed "24s/.*/\"$ct_tname\"/" file1.sas > file2.sas

回答by twalberg

Shell variables are not expanded inside single quotes. Try this instead:

Shell 变量不会在单引号内展开。试试这个:

sed "24s/.*/\"$ct_tname\"/" file1.sas > file2.sas

回答by Skippy le Grand Gourou

Other answers focus on the use of escaped double quotes in their examples. Note that this is not always what you want?:

其他答案侧重于在其示例中使用转义双引号。请注意,这并不总是您想要的?:

$ FOO="auie"; echo foo123bar|sed "s/123/\"$FOO\"/"
foo"auie"bar
$ FOO="auie"; echo foo123bar|sed "s/123/$FOO/"
fooauiebar
$ FOO="auie"; echo fooauiebar|sed "s/\"$FOO\"/123/"
fooauiebar
$ FOO="auie"; echo fooauiebar|sed "s/$FOO/123/"
foo123bar