Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要替换变量中双引号内的每个空格:
VAR='"this is my problem" but not yours'
现在我必须用'[[:space]]+'替换“这是我的问题”中的空格(可能连续不止一个)。我的外壳是busybox。最简单的方法是什么?
谢谢你。
试试这个:echo $var | sed -re 's|.*("[^"]*").*|\1|g' | sed -re 's|\s|[[:space:]]+|g'。第一个 sed 是额外的双引号部分,第二个 sed 是把双引号里面的空格变成 [[:space:]]+。
echo $var | sed -re 's|.*("[^"]*").*|\1|g' | sed -re 's|\s|[[:space:]]+|g'
PS。如果您从一开始就将评论中的所有内容都放入原始问题中,您可以节省两个小时 :)