的Unix shell - 替换字符串
空白的所有发生的最好的方式,我有一个像的Unix shell - 替换字符串
'abc', '<<some string with space>>', 'xyz' 字符串我希望得到一个字符串象下面这样: -
'abc', '<<some_string_with_space>>', 'xyz' 回答:
只需使用awk :
s="'abc', '<<some string with space>>', 'xyz'" awk -F', ' '{ gsub(/[[:space:]]+/,"_",$2) }1' OFS=', ' <<<"$s"
输出:
'abc', '<<some_string_with_space>>', 'xyz' 回答:
使用awk的gsub和正则表达式\ s来替换空格字符。
mystr = "my sentence is this!" gsub(/\s/,"_",mystr)
print mystr
回答:
如果你的问题涉及更换空间的广义问题_里面只有单引号字符串'...'而不是在其他地方, 我想解决这个使用Perl,按照这样的逻辑:
- 对于输入的每个
'...'(使用正则表达式:'[^']+') - 执行替换所有空间用
_的函数
像这样:
echo "'abc', '<<some string with space>>', 'xyz'" |\ perl -pe 'sub r { $_ = @_[0]; s/ +/_/g; return $_; }; s/'"'[^']+'"'/r($&)/ge'
回答:
您可以使用SED太
echo "'abc', '<<some string with space>>', 'xyz'" | sed s'/ /_/g;s/,_/, /g' 回答:
这可能为你工作(GNU SED):
sed ":a;s/^\(\('[^']*',\s*\)*'[^' ]*\) /\1_/;ta" file 或许更安全:
sed ':a;s/^\(\('\''[^'\'']*'\'',\s*\)*'\''[^'\'' ]*\) /\1_/;ta' file 以上是 的Unix shell - 替换字符串 的全部内容, 来源链接: utcz.com/qa/259948.html

