庆典:在sed命令中使用变量 [英] bash: use a variable in a sed command
问题描述
我似乎无法在sed命令使用一个变量,例如:
I can't seem to use a variable in a sed command, for example:
sed "24s/.*/"$ct_tname"/" file1.sas > file2.sas
我想$ ct_tname的变量,而不是从字面上ct_tname $这是我一直得到。
任何人都知道如何得到这个工作?
I want $ct_tname the variable, not literally $ct_tname which is what I keep getting. 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_"$1".csv";"
下面,$ 1在我的bash脚本的开始(SED正在bash脚本内运行)。
Here, $1 is the argument passed in at the start of my bash script (sed is being run inside a bash script).
这不会运行成功,但它确实运行,如果我取代ct_fname到
This doesn't run successful, but it does run if I replace ct_fname to
ct_fname="%let table=ct_$1;"
有没有办法拿到第一ct_fname要顺利通过?
Is there a way to get the first ct_fname to be passed successfully?
推荐答案
您需要用双引号(),而不是单引号(
)。
单引号通过他们的内容从字面上看,没有翻译变量(扩展)。
you need to use double quotes ("
) instead of single quotes ('
).
single quotes pass their content literally, without translating variables (expansion).
尝试
sed "24s/.*/\"$ct_tname\"/" file1.sas > file2.sas
顺便说一句,如果你打算要编辑一个文件(即如果的 file2.sas 的是一个临时文件),你应该使用 ED
来代替。
btw, if you're going to be editing a file (that is if file2.sas is a temporary file), you should be using ed
instead.
这篇关于庆典:在sed命令中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!