我无法弄清楚如何使这项工作:
我有:
icon-braille icon-bookmark-empty icon-blogger icon-adult icon-address-book
而且我要:
'icon-braille','icon-bookmark-empty','icon-blogger','icon-adult','icon-address-book'
这是OSX
友好的sed "s/ /','/g;s/^/'/;s/$/'/"
:
$ echo "icon-braille icon-bookmark-empty" | sed "s/ /','/g;s/^/'/;s/$/'/"
'icon-braille','icon-bookmark-empty'
使用 shell 和 sed:
echo "'"$(sed "s/ /','/g")"'"
例子:
$ echo "'"$(sed "s/ /','/g")"'"
icon-braille icon-bookmark-empty icon-blogger icon-adult icon-address-book
'icon-braille','icon-bookmark-empty','icon-blogger','icon-adult','icon-address-book'
第一行被插入,第二行——产生。
$ echo "icon-braille icon-bookmark-empty icon-blogger icon-adult icon-address-book" | sed -e "s/ \+/','/g" -e "s/^/'/" -e "s/$/'/"
'icon-braille','icon-bookmark-empty','icon-blogger','icon-adult','icon-address-book'
您可以只使用外壳(假设是 bash)
$ list="icon-braille icon-bookmark-empty icon-blogger icon-adult icon-address-book"
$ result=""; sep=""
$ for word in $list; do result+=$sep\'$word\'; sep=,; done
$ echo "$result"
'icon-braille','icon-bookmark-empty','icon-blogger','icon-adult','icon-address-book'