壳牌:如何替换文本的东西与保存的变量值 [英] Shell: How to replace something of a text with the saved variable value
问题描述
我救了shell命令的值:
例如:
I saved a value by shell command: For example:
timeoutvalue=`echo "timeout=2.0"`
和我不得不从另一个文本用grep超时值,以及与此timeoutvalue替换它。
例如,文本egtest:
And I have to grep the value of timeout from another text, and replace it with this timeoutvalue. For example, the text is egtest:
<host ip="255.255.255.0" name="testhost" description="hostfortest" connection="xmlrpc" timeout=4.0/>
我想从上面的文字用grep超时值替换超时值= 4.0
按超时= 2.0
(我保存为变量的值通过shell命令)。
I want to grep the value of timeout from the above text and replace the value of timeout=4.0
by timeout=2.0
(the value I saved as variable by shell command).
我的问题是:
(1)一个简单的测试,我想更换超时= 4.0与$ timeoutvalue值。
所以我的命令是:
My problem is: (1)for a simple test, I want to replace the value of timeout=4.0 with $timeoutvalue. So my command is:
SED的/超时= 4.0 / $ timeoutvalue / G'egtext
。
但似乎成了文本:
<host ip="255.255.255.0" name="testhost" description="hostfortest" connection="xmlrpc" $timeoutvalue/>
有谁告诉我为什么是错误的?
Could anybody tell me why it is wrong?
(2)对于这个问题,我得先grep的超时= 4.0
从文本,并替换值 4.0
来保存的变量值(在我的例子:2.0)。
我想,但我不知道是哪一个命令来实现这一(AWK?sed的?)
由于提交的文本是不特定的,(例如,在本文中,超时是在 $ 5
,但也许在另一个文本,它可能变成 $ 6个
)。我的意思是它可以变成:
(2)For this problem, I have to first grep timeout=4.0
from the text, and replace the value 4.0
to the saved variable value(in my example:2.0).
I thought but I don't know which command to use to realize this(awk? sed?)
Because the filed of the text isn't certain,(for example, in this text, timeout is in $5
, but maybe in another text, it maybe changed into $6
). I mean it may be changed into:
<host ip="255.255.255.0" name="testhost" description="hostfortest" connection="xmlrpc" type="onebox" timeout=4.0/>
任何人可以帮助我?
Could anybody help me with this?
推荐答案
单引号不允许变量替换,所以使用双引号:
Single quote does not allow variable interpolation, so use double quotes as:
sed "s/timeout=4.0/$timeoutvalue/g" egtext
编辑:
考虑
who='world'
echo "Hello $who" # prints Hello world
echo 'Hello $who" # prints Hello $who
所看到单引号不允许变量替换。它把 $谁
字面上一美元,然后按谁
。但是,双引号行为不同,它认为当 $谁
它意识到谁
是一个变量,并替换 $谁
与它的价值。
as seen single quotes does not allow variable substitution. It treats $who
literally as a dollar followed by who
. But double quote behaves differently, when it sees $who
it realizes that who
is a variable and replaces $who
with its value.
这篇关于壳牌:如何替换文本的东西与保存的变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!