2017-03-22 77 views
0

改变特定位置的字符我有我想以编程方式改变设置不同的参数文件:如何使用shell变量

$ cat ex.dat 
line 1 
here's another line 
this is the number I want to change --> 1e-11 
more lines 

我想改变1e-11的价值观5.1020.。类似这样的:

mkd() 
{ 
    mkdir $1; 
    cd $1 
} 
for j in 1 2 3 4 5 ; do 
    mkd WS$j 
    cp ../ex.dat . 
    sed -ie '3s/1e-11/${j}./' ex.dat 
    cd .. 
done 

我的方法不起作用。我如何将1e-11替换为$j的值?

$ cat WS3/ex.dat 
line 1 
here's another line 
this is the number I want to change --> ${j}. 
more lines 
+0

你是什么意思*“不工作”*?解释具体行为对其他人有帮助...... –

+3

_Double-quote_'sed'脚本确保'$ {j}'被shell扩展。 '-ie'也会创建一个后缀为'e'的备份文件,也许你打算使用'-i -e',或者简单地使用'-i'。另外,一般情况下双引号所有shell变量引用,以防止不必要的修改。 – mklement0

回答

1

您可以使用双引号在你的sed和$ J键扩展$ {}Ĵ没有大括号应该点之前工作得很好。

sed -ie "3s/1e-11/$j./" ex.dat 
2

尝试:

sed -ie '3s/1e-11/'"${j}"'./' ex.dat 

内单引号的外壳不被其值替换变量。