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:


utilizeawkTry the following three ways:

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

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

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

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

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