When a line ends with a comma, you're able to join it with the following line, by removing the line-break(s) after the comma. (But your regex does it wrong: it won't apply at all on LF-style data, it won't handle extra blank lines properly for CRLF-style data, and it removes the comma, which should be kept.)
When a line begins with a comma, you want to join it to the previous line, but the previous line has already been processed and written to output, so it's too late to fix that.
So, don't do it one line at a time - process the whole file as a single string:
Those regexes preserve the commas, and handle any number of consecutive line breaks before or after a comma (for both LF and CRLF data).perl -e '$/=undef; $_=<>; s/,[\r\n]+/,/g; s/[\r\n]+,/,/g; print' infil +e > infile.fxd
(Note that I'm redirecting output to a different file, rather than replacing the original - that makes it easy to "try, try again" for cases like this, where you seldom get it right the first time. Once you get it right, then you can rename the output to replace the input.)
(Update: P.S.: Welcome to the Monastery!)
(Updated again to add remarks about LF vs. CRLF data)
In reply to Re: Auto correct a csv file
by graff
in thread Auto correct a csv file
by karthikAk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |