在 sed 命令中使用变量 [英] Use a variable in a sed command

查看:24
本文介绍了在 sed 命令中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法在 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 successfully, but it does run if I replace ct_fname with

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆