如何使用regex格式化列表

时间:2022-10-01 08:54:49

I cannot figure out how to make this work:

我想不出该怎么做:

I have:

我有:

icon-braille icon-bookmark-empty icon-blogger icon-adult icon-address-book

icon-braille icon-bookmark-empty icon-blogger icon icon-adult icon-address-book

And I want:

我希望:

'icon-braille','icon-bookmark-empty','icon-blogger','icon-adult','icon-address-book'

“icon-braille”、“icon-bookmark-empty’,‘icon-blogger’,‘icon-adult’,‘icon-address-book’

4 个解决方案

#1


2  

This is OSX friendly sed "s/ /','/g;s/^/'/;s/$/'/":

这是OSX友好对话”/ /,/ g;年代/ ^ / /,/美元/ /”:

$ echo "icon-braille icon-bookmark-empty" | sed "s/ /','/g;s/^/'/;s/$/'/" 
'icon-braille','icon-bookmark-empty'

#2


1  

With shell and sed:

与壳牌和对话:

echo "'"$(sed "s/ /','/g")"'"

Example:

例子:

$ 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'

The first line was inserted, the second — produced.

插入第一行,生成第二行。

#3


0  

 $ 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'

#4


0  

You can just use the shell (assuming bash)

您可以使用shell(假设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'

#1


2  

This is OSX friendly sed "s/ /','/g;s/^/'/;s/$/'/":

这是OSX友好对话”/ /,/ g;年代/ ^ / /,/美元/ /”:

$ echo "icon-braille icon-bookmark-empty" | sed "s/ /','/g;s/^/'/;s/$/'/" 
'icon-braille','icon-bookmark-empty'

#2


1  

With shell and sed:

与壳牌和对话:

echo "'"$(sed "s/ /','/g")"'"

Example:

例子:

$ 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'

The first line was inserted, the second — produced.

插入第一行,生成第二行。

#3


0  

 $ 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'

#4


0  

You can just use the shell (assuming bash)

您可以使用shell(假设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'