Sed Pattern for inserting a comma between double quotes

I am very new to the Regex sed pattern and adhered to the requirement. I have several records that I pull from the database

"Name" "Age" "From ABC" "12"

I am now exporting it to CSV using Comma Separated. I am using 's/[[:space:]]\+/,/g' This Regex

Which gives me the result as

"Name", "Age", "From, ABC", "12"

This is fine, except for the third column where he pasted , into the space now I want to insert , between double quotes.

Can someone help me for the same thing?

With 's/"[[:space:]]\+"/","/g' Regex i Am can get "Name","Age","From,ABC","12" But it fails when I am "Name","","From,ABC","12" it gives "Name",",","From,ABC","12"

Is there any other way to counter this?

+5
source share
3 answers

I would do this:

 echo \"Name\" \"Age\" \"From ABC\" \"12\" | sed -e 's/"[[:space:]]\+"/","/g' 

which returns

 "Name","Age","From ABC","12" 

change

if you want the empty lines " " also match, you can do this:

 $ echo \"Name\" \" \" \"Age\" \"From ABC\" \"12\" | sed -e 's/\("[[:alnum:][:space:]]*"\)/\1,/g' \ -e 's/",[[:space:]]\+/",/g' \ -e 's/,[[:space:]]\?$//g' 

which returns

 "Name"," ","Age","From ABC","12" 

The first rule matches quoted strings (which may also be empty), the second rule removes the spaces between , and the following, " third rule removes the last,.

+2
source

Replace " " with "," :

 sed 's/"[[:space:]]\+"/","/g' 

Edited to capture more than one place

+6
source

This may work for you (GNU sed):

 sed -r 's/\s+$//;s/("[^"]*(\\.[^"]*)*")\s+/\1,/g' file 

Remove the space at the end of the line. Then globally replace any pair of double-quoted strings followed by space (s), with a string followed by a comma.

NB This refers to quoted characters (including double quotes) in quoted strings.

0
source

Source: https://habr.com/ru/post/1275240/


All Articles