sed命令不接受带有'.'的子字符串在ksh中 [英] sed command not accepting sub-string with '.' in ksh
问题描述
我正在编写一个ksh脚本,该脚本将包含@sys
子字符串的路径变量处理为其相应的OS名称:
I am writing a ksh script which processes a path variable containing @sys
substring to its corresponding OS name:
如果build_type
是64位版本,我们需要在osver
的末尾附加.64
.
If build_type
is is of 64bit version we need to append .64
at the end of osver
.
例如如果opsys=amd64_linux26_rh5
(红帽企业版Linux 5)和build_type=dbg64
,osver
应该是amd64_linux26_rh5.64
.
e.g. if opsys=amd64_linux26_rh5
(Red Hat Enterprise Linux 5) and build_type=dbg64
, osver
should be amd64_linux26_rh5.64
.
现在,我想将file_path
变量中的@sys
替换为其对应的OS版本,作为子字符串,即amd64_linux26_rh5.64
.
Now, I want to replace @sys
present in file_path
variable with its corresponding OS version as a substring i.e. amd64_linux26_rh5.64
.
但sed
似乎正在忽略.不知何故,唯一的amd64_linux26_rh5
是替换@sys
,省略了.64
部分.
But it looks like sed
is ignoring . somehow and the only amd64_linux26_rh5
is replacing @sys
omitting the .64
part.
#!/bin/ksh
file_path="/xxx/xxx/xxx/@sys/xxx"
sys="@sys"
bld_type="dbg64"
opsys="amd64_linux26_rh5"
bitver=".64"
if [[ ${bld_type} = @(*64) ]]
then
osver="${opsys}${bitver}"
fi
echo "osver: $osver"
echo "old file_path is : $file_path"
file_path=$(echo "$file_path" | sed "s/$sys/${osver}/")
echo "new file_path is : $file_path"
对应的输出:
osver: amd64_linux26_rh5.64
old file_path is : /xxx/xxx/xxx/amd64_linux26_rh5/xxx
new file_path is : /xxx/xxx/xxx/amd64_linux26_rh5/xxx
我们正在尝试将file_path
更改为/xxx/xxx/xxx/amd64_linux26_rh5.64/xxx
.在这种情况下,请提出使用sed
的适当方法.
We are trying to change file_path
to /xxx/xxx/xxx/amd64_linux26_rh5.64/xxx
. Please suggest an appropriate way use sed
in this case.
推荐答案
输出显示时
old file_path is : /xxx/xxx/xxx/amd64_linux26_rh5/xxx
然后在复制粘贴代码时出了点问题.
在您的代码中,file_path
设置为
then something went wrong in copy-pasting the code.
In your code the file_path
is set to
file_path="/xxx/xxx/xxx/@sys/xxx"
,并且永远不会在echo
之前更改.因此,您的代码似乎与您显示的代码不同.
当您在
and is never changed before the echo
. So your code seems to be different from what you show.
When you start after the
echo "old file_path is : $file_path"
输出有意义.
在您的sed命令中
the output makes sense.
In your sed command
file_path=$(echo "$file_path" | sed "s/$sys/${osver}/")
sed在字符串/xxx/xxx/xxx/amd64_linux26_rh5/xxx
中找不到@sys
,并且old_string
保持不变.
sed will not find @sys
in the string /xxx/xxx/xxx/amd64_linux26_rh5/xxx
, and the old_string
remains unchanged.
这篇关于sed命令不接受带有'.'的子字符串在ksh中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!