在Shell脚本中使用带有sed的变量

时间:2021-01-24 23:48:26

I'm writing a shell script to edit Change-Set attributes of aegis. The command I'm using is:

我正在编写一个shell脚本来编辑aegis的Change-Set属性。我正在使用的命令是:

aegis -Change_Attributes -Edit

which opens up a vi editor to carry out the changes. I want to do a search and replace:

这打开了一个vi编辑器来执行更改。我想做一个搜索并替换:

s/brief_description \= \"none\"\;/brief_description \=

\"test\"/g

Can I pass these directly to the open vi instance via the script without typing in any of it? I want to save the document (:wq) after editing it.

我可以通过脚本将这些直接传递给open vi实例而无需输入任何内容吗?我想在编辑后保存文档(:wq)。

P.S. The file is a temporary file created when executing the command so I don't know the original path

附:该文件是执行命令时创建的临时文件,因此我不知道原始路径

Edit: I could used sed in this case:

编辑:在这种情况下我可以使用sed:

sed -e 's/brief_description\ \=\ \"none\"\;/brief_description\ \=\

\"test\"\;/g'

The solution (inelegant hack??) would be to "cat" the output from aegis (setenv VISUAL cat), modify the out put stream with the above command and save it to a temp file, and use :

解决方案(不优雅的黑客??)将“cat”来自aegis(setenv VISUAL cat)的输出,使用上述命令修改输出流并将其保存到临时文件,并使用:

aegis -change_attributes -file <temp file>

EDIT2: I've almost got it to work. But there's a problem with the way I use sed

编辑2:我几乎得到了它的工作。但是我使用sed的方式存在问题

I have the following line in my script:

我的脚本中有以下行:

sed -i 's/brief_description\ \=\ \"none\"\;/brief_description\ \=\ \"${DESC}\"\;/g' temp_next.txt

But the $DESC variable does not evaluate to its value and the out put is given as:

但$ DESC变量不评估其值,输出值为:

brief_description = "${DESC}";

brief_description =“$ {DESC}”;

How can I pass DESC to sed that it would evaluate to it's actual value?

我怎样才能将DESC传递给sed,它会评估它的实际值?

EDIT3:

Using

sed -i 's%brief_description\ \=\ \"none\"\;%brief_description\ \=\ \"'"$DESC"'\"\;%g' temp_next.txt

worked. I replace the normal delimiter (/) with % and put the environment variable in double quotes.

工作。我用%替换正常分隔符(/)并将环境变量放在双引号中。

3 个解决方案

#1


You don't need to know the path - the aegis app will supply that. You need to change the environment variable that specifies what editor aegis uses to point at a script, and in that script use the sed stream editor to perform your edits.

您不需要知道路径 - aegis应用程序将提供该路径。您需要更改环境变量,该变量指定编辑器用于指向脚本的编辑器,并在该脚本中使用sed流编辑器执行编辑。

Edit: Regarding your variable name expansion problem, change the set of single quotes enclosing the whole sed substitution expression to double quotes. Variable substitution is turned off by single quotes.

编辑:关于变量名称扩展问题,将包含整个sed替换表达式的单引号集更改为双引号。单引号关闭变量替换。

#2


If you pass the desired command in via the -c option then vi will execute the command immediately after starting the edit session, e.g.

如果通过-c选项传递所需的命令,则vi将在开始编辑会话后立即执行命令,例如,

vi -c 's/brief_description \= \"none\"\;/brief_description \= \"test\"/g' my_file

Oops. I forgot to say that the command is interpreted as an "ex" command, i.e. at the colon, so the command you've provided should work.

哎呀。我忘了说命令被解释为“ex”命令,即在冒号处,所以你提供的命令应该有效。

HTH

cheers,

#3


I don't think that vanilla vi will do this, although vim might be able to this under control of a custom .vimrc. A better way would be to do the changes with sed and then open vi on the result.

我不认为vanilla vi会这样做,虽然vim可能能够在自定义.vimrc的控制下。更好的方法是使用sed进行更改,然后在结果上打开vi。

#1


You don't need to know the path - the aegis app will supply that. You need to change the environment variable that specifies what editor aegis uses to point at a script, and in that script use the sed stream editor to perform your edits.

您不需要知道路径 - aegis应用程序将提供该路径。您需要更改环境变量,该变量指定编辑器用于指向脚本的编辑器,并在该脚本中使用sed流编辑器执行编辑。

Edit: Regarding your variable name expansion problem, change the set of single quotes enclosing the whole sed substitution expression to double quotes. Variable substitution is turned off by single quotes.

编辑:关于变量名称扩展问题,将包含整个sed替换表达式的单引号集更改为双引号。单引号关闭变量替换。

#2


If you pass the desired command in via the -c option then vi will execute the command immediately after starting the edit session, e.g.

如果通过-c选项传递所需的命令,则vi将在开始编辑会话后立即执行命令,例如,

vi -c 's/brief_description \= \"none\"\;/brief_description \= \"test\"/g' my_file

Oops. I forgot to say that the command is interpreted as an "ex" command, i.e. at the colon, so the command you've provided should work.

哎呀。我忘了说命令被解释为“ex”命令,即在冒号处,所以你提供的命令应该有效。

HTH

cheers,

#3


I don't think that vanilla vi will do this, although vim might be able to this under control of a custom .vimrc. A better way would be to do the changes with sed and then open vi on the result.

我不认为vanilla vi会这样做,虽然vim可能能够在自定义.vimrc的控制下。更好的方法是使用sed进行更改,然后在结果上打开vi。