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

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

问题描述

  sed24s /.* /$ ct_tname/file1.sas> file2.sas 

我想要 $ ct_tname 变量,不是字面上 $ ct_tname ,这是我一直在得到的。



任何人都知道如何让这个工作?



问题实际上更复杂,我省略了一些信息。

  ct_fname =%let outputfile =/ user / ct _$ 1.csv; 

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



这不能成功运行,但如果我替换 ct_fname with

  ct_fname =%let table = ct_ $ 1; 

有没有办法获得第一个 ct_fname 要成功通过

解决方案

需要使用双引号()而不是单引号('
单引号直接传递其内容,无需翻译变量(扩展)。



尝试

  sed24s /.*/ \$ ct_tname\/file1 .sas> file2.sas 

btw,如果你要编辑一个文件是如果 file2.sas 是一个临时文件),您应该使用 ed


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

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

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";"

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

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

ct_fname="%let table=ct_$1;"

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).

try

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

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天全站免登陆