2014-11-05 193 views
1

我需要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

您可以使用sed

p='*abc*def*' 
echo "$p" | sed 's/\*/.*/g' 
.*abc.*def.* 

确保您使用的是正确的引用,以避免外壳扩展*

+0

谢谢!它解决了我的问题 – BattleArray61 2014-11-05 06:14:35

+0

不客气,很高兴它解决了。 – anubhava 2014-11-05 06:14:59

0

尝试此代码 - >

a='abc*def' 
b=. 
c="${a/'*'/$b}"