Linux sed中的环境变量替换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/584894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Environment variable substitution in sed
提问by RomanM
If I run these commands from a script:
如果我从脚本运行这些命令:
#my.sh
PWD=bla
sed 's/xxx/'$PWD'/'
...
$ ./my.sh
xxx
bla
it is fine.
没事。
But, if I run:
但是,如果我运行:
#my.sh
sed 's/xxx/'$PWD'/'
...
$ ./my.sh
$ sed: -e expression #1, char 8: Unknown option to `s'
I read in tutorials that to substitute environment variables from shell you need to stop, and 'out quote' the $varname
part so that it is not substituted directly, which is what I did, and which works only if the variable is defined immediately before.
我在教程中读到要从 shell 替换环境变量,您需要停止,并“引用”该$varname
部分,以便它不会被直接替换,这就是我所做的,并且仅当变量在之前定义时才有效。
How can I get sed to recognize a $var
as an environment variable as it is defined in the shell?
我怎样才能让 sed 将 a 识别$var
为 shell 中定义的环境变量?
采纳答案by Norman Ramsey
Your two examples look identical, which makes problems hard to diagnose. Potential problems:
您的两个示例看起来相同,这使得问题难以诊断。潜在问题:
You may need double quotes, as in
sed 's/xxx/'"$PWD"'/'
$PWD
may contain a slash, in which case you need to find a character notcontained in$PWD
to use as a delimiter.
您可能需要双引号,如
sed 's/xxx/'"$PWD"'/'
$PWD
可能包含斜杠,在这种情况下,您需要找到未包含的字符$PWD
用作分隔符。
To nail both issues at once, perhaps
一次解决这两个问题,也许
sed 's@xxx@'"$PWD"'@'
回答by Eddie
With your question edit, I see your problem. Let's say the current directory is /home/yourname
... in this case, your command below:
通过您的问题编辑,我看到了您的问题。假设当前目录是/home/yourname
... 在这种情况下,您的命令如下:
sed 's/xxx/'$PWD'/'
will be expanded to
将扩展到
sed `s/xxx//home/yourname//
which is not valid. You need to put a \
character in front of each /
in your $PWD if you want to do this.
这是无效的。如果您想这样做,您需要在 $PWD 中\
的每个字符之前放置一个字符/
。
回答by Jeach
In addition to Norman Ramsey's answer, I'd like to add that you can double-quote the entire string (which may make the statement more readable and less error prone).
除了 Norman Ramsey 的回答之外,我想补充一点,您可以用双引号引用整个字符串(这可能使语句更具可读性且不易出错)。
So if you want to search for 'foo' and replace it with the content of $BAR, you can enclose the sed command in double-quotes.
因此,如果您想搜索 'foo' 并将其替换为 $BAR 的内容,您可以将 sed 命令用双引号括起来。
sed 's/foo/$BAR/g'
sed "s/foo/$BAR/g"
In the first, $BAR will not expand correctly while in the second $BAR will expand correctly.
在第一个中,$BAR 不会正确扩展,而在第二个 $BAR 中将正确扩展。
回答by adeger
Actually, the simplest thing (in gnu sed at least) is to use a different separator for the sed substitution (s) command. So instead of s/pattern/'$mypath'/ being expanded to s/pattern//my/path/ which will of course confuse the s command, use s!pattern!'$mypath'! which will be expanded to s!pattern!/my/path! I've used the bang (!) character (or use anything you like) which avoids the usual, but-by-no-means-your-only-choice forward slash as the separator.
实际上,最简单的事情(至少在 gnu sed 中)是为 sed 替换 (s) 命令使用不同的分隔符。因此,不要将 s/pattern/'$mypath'/ 扩展为 s/pattern//my/path/ 这当然会混淆 s 命令,而是使用 s!pattern!'$mypath'!这将扩展为 s!pattern!/my/path! 我使用了 bang (!) 字符(或使用任何你喜欢的东西),它避免了通常的,但绝不是你唯一选择的正斜杠作为分隔符。
回答by Paulo Fidalgo
You can use other characters besides "/" in substitution:
您可以使用除“/”之外的其他字符进行替换:
sed "s###g" -i FILE
回答by aniskop
I had similar problem, I had a list and I have to build a SQL script based on template (that contained @INPUT@
as element to replace):
我有类似的问题,我有一个列表,我必须基于模板(包含@INPUT@
要替换的元素)构建一个 SQL 脚本:
for i in LIST
do
awk "sub(/\@INPUT\@/,\"${i}\");" template.sql >> output
done
回答by PsychoData
VAR=8675309
echo "abcde:jhdfj$jhbsfiy/.hghi$jh:12345:dgve::" |\
sed 's/:[0-9]*:/:'$VAR':/1'
where VAR contains what you want to replace the field with
其中 VAR 包含您要替换字段的内容
回答by Mamadou Lamine Diatta
Dealing with VARIABLES within sed
处理 sed 中的变量
[root@gislab00207 ldom]# echo domainname: None > /tmp/1.txt
[root@gislab00207 ldom]# cat /tmp/1.txt
domainname: None
[root@gislab00207 ldom]# echo ${DOMAIN_NAME}
dcsw-79-98vm.us.oracle.com
[root@gislab00207 ldom]# cat /tmp/1.txt | sed -e 's/domainname: None/domainname: ${DOMAIN_NAME}/g'
--- Below is the result -- very funny.
domainname: ${DOMAIN_NAME}
--- You need to single quote your variable like this ...
[root@gislab00207 ldom]# cat /tmp/1.txt | sed -e 's/domainname: None/domainname: '${DOMAIN_NAME}'/g'
--- The right result is below
domainname: dcsw-79-98vm.us.oracle.com
回答by Thales Ceolin
Another easy alternative:
另一个简单的选择:
Since $PWD
will usually contain a slash /
, use |
instead of /
for the sed statement:
由于$PWD
通常会包含一个斜杠/
,请使用|
代替/
sed 语句:
sed -e "s|xxx|$PWD|"
回答by yurenchen
bad way: change delimiter
坏方法:更改分隔符
sed 's/xxx/'"$PWD"'/'
sed 's:xxx:'"$PWD"':'
sed 's@xxx@'"$PWD"'@'
maybe those not the final answer,
也许那些不是最终答案,
you can not known what character will occur in $PWD
, /
:
OR @
.
您无法知道$PWD
, /
:
OR 中会出现什么字符@
。
the good way is replace the special character in $PWD
.
好的方法是替换$PWD
.
good way: escape delimiter
好方法:转义分隔符
for example:
例如:
use /
as delimiter
使用/
作为分隔符
echo ${url//\//\/}
x.com:80\/aa\/bb\/aa.js
echo ${url//\//\/}
x.com:80/aa/bb/aa.js
echo "${url//\//\/}"
x.com:80\/aa\/bb\/aa.js
echo $tmp | sed "s/URL/${url//\//\/}/"
<a href="x.com:80/aa/bb/aa.js">URL</a>
echo $tmp | sed "s/URL/${url//\//\/}/"
<a href="x.com:80/aa/bb/aa.js">URL</a>
OR
或者
use :
as delimiter (more readable than /
)
使用:
作为分隔符(除了更易读/
)
echo ${url//:/\:}
x.com:80/aa/bb/aa.js
echo "${url//:/\:}"
x.com\:80/aa/bb/aa.js
echo $tmp | sed "s:URL:${url//:/\:}:g"
<a href="x.com:80/aa/bb/aa.js">x.com:80/aa/bb/aa.js</a>