2014-10-27 144 views
1

我在文件中的一行这种替换文本使用sed

integer, parameter :: L = 2 !Size of the system 

其中数字2可以是任意整数数量。我想使用sed找到这一行,并用另一个替换该号码的值。

我想:

$ L=5 
$ sed -i '/L = /c\integer, parameter :: L = $L !Size of the system' moduleselfcons.f90 

但在我修改过的文件我在最后:

integer, parameter :: L = $ !Size of the system 

,而不是:

integer, parameter :: L = 5 !Size of the system 

问题是什么?

+0

[在bash shell脚本的命令中扩展单引号内变量的可能重复](http://stackoverflow.com/questions/13799789/expansion-of-variable-inside-single-quotes-in-a- command-in-bash-shell-script) – 2014-10-27 15:09:58

回答

1

您应该使用双引号(")而不是单引号(')。

但我认为你可以缩短表达:

L=5 
sed "s/L = [0-9]*/L = $L/" file 

如果这是你想要的输出加上-i标志就地编辑。

+0

@ user3368447''''必须用'\!'和双引号试试。 – chaos 2014-10-27 15:36:14