0

Similar to this question here, I would like to have double quotes around each variable (which are sometimes strings, sometimes numbers) separated by commas in a file without spaces, such as below:

variable1,12345,variable2,AA3

What's the easiest way to use BASH (on macOS 10.14.6) for adding quotation marks to each word?

The end result should look like:

"variable1","12345","variable2","AA3"
Til Hund
  • 1,543
  • 5
  • 21
  • 37
  • 1
    Why don't you use any of the answers to your linked question? – Cyrus Oct 07 '19 at 19:51
  • Because I do not know ‘sed’ or alike to do the necessary adjustments to my specific case (no spaces between commas). – Til Hund Oct 07 '19 at 20:18

2 Answers2

3

Using single sed command:

s='variable1,12345,variable2,AA3'
sed -E 's/[^,]+/"&"/g' <<< "$s"

"variable1","12345","variable2","AA3"
anubhava
  • 761,203
  • 64
  • 569
  • 643
1

You can use 'sed' to insert '"' at the start of the line, end of line, and replace every '.' with '","'.

sed -e 's/^/"/' -e 's/$/"/' -e 's/,/","/g'
dash-o
  • 13,723
  • 1
  • 10
  • 37