且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

逗号分隔的值转换为单引号和逗号分隔的值

更新时间:2022-12-10 07:51:09

在开始(^)处添加单引号&结束($),并使用sed和3个表达式(使用-e选项指定它们),用quote-comma-quote替换逗号(就像您所做的那样):

Add a single quote at start (^) & end ($), and replace comma by quote-comma-quote (like you did) using sed with 3 expressions (using -e option to specify them):

echo  abc,defg,hijklm,op,qrs,tuv | sed -e "s/^/'/" -e "s/\$/'/" -e "s/,/',\'/g" 

(另:用双引号保护单引号)

(also: protect single quotes with double quotes)

导致:

'abc','defg','hijklm','op','qrs','tuv'