我对bash脚本非常陌生。如何转换同时包含词组和单词的字符串:

flowers yellow "beautiful yellow flowers" nature colors


到每个字符串之间都带有引号的字符串,并在它们之间添加逗号:

"flowers", "yellow", "beautiful yellow flowers", "nature", "colors"

最佳答案

您可以通过下面的Perl一线命令执行此操作,

$ s='flowers yellow "beautiful yellow flowers" nature colors'
$ perl -pe 's/"[^"]*"(*SKIP)(*F)|(\S+)/"\1",/g; s/,$//; s/"(?= *")/",/g' <<< "$s"
"flowers", "yellow", "beautiful yellow flowers", "nature", "colors"

关于regex - 如何将引号括在字符串中的单个单词周围,而不是放在已经有引号的子字符串周围?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26200024/

10-12 15:33