Shell scripts add double quotes to comma-separated content

  linux, question

There is a filea, its content is:

1, 2, 3
$ head a
 1, 2, 3

I want to convert the above file into the following format for sql conditional query:

"1","2","3"

utilizeawkTry the following three ways:

# An empty line has been added
 $ awk -v 'RS= ' {print} a
 1,
 2,
 3
# Failed to delete blank line in this way
 $ awk -v 'RS= ' '{gsub(/^$/,"");  print}' a
 1,
 2,
 3
There is a line break after #3
 $ awk -v 'RS= ' 'ORS=","{gsub(/,$/,"");  print "\""$0"\""}' a
 "1","2","3
 ",%

I wonder if there is a better way to realize this function.

ruby -e 'print $_.gsub(/\d/){|i| %("#{i}")} while gets' a

Explanation:
1,ruby -eExecute the following string command
2,$_showgetsThe content of
3,gsubUsed for string matching replacement.
4,%(...), used to generate a string, otherwise to “”#{i} “”,more ugly
5,gets, reads in standard input, or a document

Or more simply sed:

sed 's,\([0-9]\),"\1",g' a

Explanation:
1,sedBy default, actions are performed on each line of the file
2,sIndicates text replacement
3,,Is a delimiter and can be;,/,!Wait
4,\(...\)Represents the’ group’ of regular expressions to facilitate the following\1Make a reverse reference
5,[0-9]Matching number
6,gIndicates to replace all matches