Sed and regex to replace ',' except for the string

I have the input of the following circuit

10,0,'string1_string2,_string3','',8,0,0,0.59,'20140101205216','20140128074836',584266915,5934

and I would like to replace all comma " ," characters with tabs using sed. The limitation is not to replace " ," inside text strings (i.e. the comma in 'string1_string2,_string3'should not be replaced by a tab). The regular expression for this is: (?!, _).

However, the following sed does not work. I also tried all shielding permutations.

sed s/",\(\?\!,_\)"/"\t"/g 

Is there any way to do this?

+3
source share
4 answers

On Mac OS X 10.9.1 you can use:

sed -E -e "s/('[^']*'|[^,]*),/\1X/g"

except that you replace X with the actual tab. For your input line, which gives:

10X0X'string1_string2,_string3'X''X8X0X0X0.59X'20140101205216'X'20140128074836'X584266915X5934

X, . GNU sed -r -E ( -E). Mac sed \t ; GNU sed . Bash Quoting ANSI-C, , sed:

sed -E -e "s/('[^']*'|[^,]*),/\1"$'\t'"/g"

( -r -E), sed; awk.

, , , , , / string, "" ( X , ).


devnull , . :

sed -E -e "s/('[^']*'|[^,]*)(,|$)/\1"$'\t'"/g; s/"$'\t'"$//"

s///g ; s/// .

+2

Perl, - lookarounds:

s="10,0,'string1_string2,_string3','',8,0,0,0.59,'20140101205216','20140128074836',584266915,5934"

perl -pe "s/,(?=(([^']*'){2})*[^']*$)/\t/g" <<< "$s"

10\t0\t'string1_string2,_string3'\t''\t8\t0\t0\t0.59\t'20140101205216'\t'20140128074836'\t584266915\t5934

PS: \t .

+1

Text::ParseWords:

perl -MText::ParseWords -n -l -e 'print join("\t", parse_line(",", 1, $_));' filename

:

10      0       'string1_string2,_string3'      ''      8       0       0       0.59    '20140101205216'        '20140128074836'        584266915       5934
+1

This seems to work if I understand your question correctly:

sed -E 's/,([^_])/\t\1/g'

Conclusion:

10  0   'string1_string2,_string3'  ''  8   0   0   0.59    '20140101205216'    '20140128074836'    584266915   5934
0
source

All Articles