<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:arial,helvetica,sans-serif;font-size:10pt"><div style="font-family: arial,helvetica,sans-serif; font-size: 10pt;">Thanks&nbsp; everyone. Here is the one-liner&nbsp; I am&nbsp; looking for!<br><span style="font-family: courier,monaco,monospace,sans-serif;">perl -ne 's/(?&lt;!,)(?:(^")|")(?!,|$)/defined($1)?$1:""/ge?print:print' in.csv &gt; out.csv</span><br>(-; I have not used my real first name 'Ge' for regex for a while ;-)<br>I believe the replace character and the delimiter can be a set of character, such as<span style="font-family: courier,monaco,monospace,sans-serif;"> ["'] [,:|].<br><br>Another question: How to use one-liner to split web log file?<br></span><br><div style="font-family: times new roman,new york,times,serif; font-size: 12pt;">----- Original Message ----<br>From: Brian Katzung &lt;briank@kappacs.com&gt;<br>To: Chicago.pm chatter
 &lt;chicago-talk@pm.org&gt;<br>Sent: Wednesday, October 24, 2007 8:33:11 PM<br>Subject: Re: [Chicago-talk] Removing Characters<br><br>tiger peng wrote:<br>&gt; You are right if it is well formated CSV file.<br>&gt; But I don't know if this is guaranteed. There is no document about
 it. I <br>&gt; just rewrite an old old uncommented/undocumented working scripts.<br>&gt; <br>&gt; The segment is just rewritten to make it little bit easier to read by
 <br>&gt; replacing the embedded 'DEL' charters with a variable populate with <br>&gt; function chr.<br>&gt; <br>&gt; Below is the best I can do. It looks better to me and can run as two <br>&gt; times faster than the old one does. I still cannot make out one-liner
 <br>&gt; for it. Can anyone get ride of the first line?<br>&gt; <br>&gt;&nbsp;  my $leadingQ=""; $leadingQ='"' if /^"/; #save the leading quote if
 it <br>&gt; is there<br>&gt;&nbsp;  s/(?&lt;!,)"(?!(,|$))//g; # remove all double quote not next to comma
 or <br>&gt; at the end of the line<br>&gt;&nbsp;  print OUTF $leadingQ, $_;<br><br>OK. Here's my one-liner version (well, two including the print) of the <br>above:<br><br>s/(?&lt;!,)(?:(^")|")(?!,|$)/defined($1)?$1:''/eg;<br>print OUTF $_;<br><br>&gt; ----- Original Message ----<br>&gt; From: Steven Lembark &lt;<a ymailto="mailto:lembark@wrkhors.com" href="mailto:lembark@wrkhors.com">lembark@wrkhors.com</a>&gt;<br>&gt; To: Chicago.pm chatter &lt;<a ymailto="mailto:chicago-talk@pm.org" href="mailto:chicago-talk@pm.org">chicago-talk@pm.org</a>&gt;<br>&gt; Sent: Wednesday, October 24, 2007 2:14:48 PM<br>&gt; Subject: Re: [Chicago-talk] Removing Characters<br>&gt; <br>&gt;&nbsp; &gt; There must be better way for removing the double quote in a CSV
 file<br>&gt;&nbsp; &gt; optionally quoted by double quote.<br>&gt;&nbsp; &gt; What I did as below is ugly and not reliable. Could anyone provide
 one<br>&gt;&nbsp; &gt; beautify line?<br>&gt;&nbsp; &gt;<br>&gt;&nbsp; &gt;&nbsp; $delimiter=chr(0227);<br>&gt;&nbsp; &gt;&nbsp; s/^"/$delimiter/g;<br>&gt;&nbsp; &gt;&nbsp; s/,"/,$delimiter/g;<br>&gt;&nbsp; &gt;&nbsp; s/"$/$delimiter/g;<br>&gt;&nbsp; &gt;&nbsp; s/",/$delimiter,/g;<br>&gt;&nbsp; &gt;&nbsp; s/"//g;<br>&gt;&nbsp; &gt;&nbsp; s/$delimiter/"/g;<br>&gt; <br>&gt; You don't seem to want all of the quotes removed,<br>&gt; only the embedded ones. If the data is well-formatted<br>&gt; then the operation above will leave you with a bunch<br>&gt; of naked backslashes in the text:<br>&gt; <br>&gt;&nbsp;  "this is a \"double quoted\" text line"<br>&gt; <br>&gt; becomes<br>&gt; <br>&gt;&nbsp;  "this is a \double quoted\ text line"<br>&gt; <br>&gt; and you probably don't want the \d or \ in your<br>&gt; result.<br>&gt; <br>&gt; If the real problem is that fate has handed you some<br>&gt; CSV data with embedded, un-escaped quotes then your<br>&gt;
 approach makes the most sense, but you'll have to<br>&gt; remove escaped quotes also:<br>&gt; <br>&gt;&nbsp;  s{ \\" }{}gx;<br>&gt; <br>&gt; will strip the \" char's. You might prefer to replace<br>&gt; them with non-delimiting quotes, e.g.,<br>&gt; <br>&gt; <br>&gt;&nbsp;  s{ \\" }{'}gx;<br>&gt; <br>&gt; All of the CSV parsing modules assume "clean" CSV<br>&gt; source (oxymoron?) so if you need to clean up botched<br>&gt; data then some iterative approach is likely to be<br>&gt; what you need.<br>&gt; <br>&gt; enjoi<br>&gt; <br>&gt; -- <br>&gt; Steven Lembark&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 85-09 90th
 Street<br><br>Here's a "smarter" but longer obfuscation:<br><br>s{((?:^|,)")((?:[^"]|\\"|"")*?)(")(?=,|$)|([^,]*)}{<br>&nbsp; (sub { my @a = @_; $a[1] =~ s/\\"|"//g;<br>&nbsp; join('', grep(defined, @a)); })-&gt;<br>&nbsp; ($1, defined($2)? $2: $4, $3)}eg;<br>print;<br><br>You're certainly at liberty to remove newlines from the code if the
 line <br>count is that important to you. :-)<br><br>This one considers a field to be quoted only if it has (anchored)
 quotes <br>at each end and internal quotes are \-escaped or doubled. All other \" <br>and " that are not anchored in a quoted field are removed.<br><br>I haven't attempted any timings.<br><br>&nbsp;  - Brian<br><br>-- <br>Brian Katzung, Kappa Computer Solutions, LLC<br>Leveraging UNIX, Linux, open source, and custom<br>software solutions for business and beyond<br>Phone: 877.367.8837 x1&nbsp; <a href="http://www.kappacs.com" target="_blank">http://www.kappacs.com</a><br><br>_______________________________________________<br>Chicago-talk mailing list<br><a ymailto="mailto:Chicago-talk@pm.org" href="mailto:Chicago-talk@pm.org">Chicago-talk@pm.org</a><br><a href="http://mail.pm.org/mailman/listinfo/chicago-talk" target="_blank">http://mail.pm.org/mailman/listinfo/chicago-talk</a><br></div><br></div></div></body></html>