我需要使用shell脚本语句将"*"
符号替换为字符串中的".*"
。例如:string1="abc*def"
转换为此string2:"abc.*def"
。我正在尝试使用这段代码,但它输出错误:
IN="abc*def"
chr=".*"
arr=$(echo ${args[@]} | tr "*" "\n")
for x in $arr
do
echo -n $x
echo -n ".*"
done
for abc*def gives abc.*def.* not correct
for abc*def* gives abc.*def.* correct
for *abc*def* gives abc.*def.* not correct
答案 0 :(得分:0)
您可以使用sed
:
p='*abc*def*'
echo "$p" | sed 's/\*/.*/g'
.*abc.*def.*
确保使用正确的引用以避免shell扩展*
答案 1 :(得分:0)
试试此代码 - >
a='abc*def'
b=.
c="${a/'*'/$b}"