Use a variable in a sed command

后端 未结 6 1357
梦谈多话
梦谈多话 2020-12-09 03:15

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_tnam

6条回答
  •  -上瘾入骨i
    2020-12-09 03:47

    The problem is that when $ct_fname is substituted, sed sees extra / separators, so

    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 sed error because there are 5 / instead of the expected 3.

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

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

提交回复
热议问题