Re: Removing rogue ciarraige returns in data file

From: rakesh sharma (sharma__r_at_hotmail.com)
Date: 08/21/03


Date: 21 Aug 2003 14:31:25 -0700


"Jonathan" <jonathan@bakerbates.SPAMGONE.com> wrote in message news:<L3J0b.9471$z7.1155128@wards.force9.net>...
> Hi,
>
> I'm having a hard time with a CSV file that has carriage returns in a postal
> address field, making the number of columns vary. Has anyone got any
> pointers on how I can mend this?
>
> For example, the data looks like this right now:
>
> field1,field2,field3,field4,field5
> field1,field2,field3,field4,field5
> field1,field2,field3
> ,field4,field5
> field1,field2,field3,field4,field5
>
> (Note the CR at the end of field3 in the column 3)
>
> I'd like to fix it so it looks like this:
>
> field1,field2,field3,field4,field5
> field1,field2,field3,field4,field5
> field1,field2,field3,field4,field5
> field1,field2,field3,field4,field5
>

sed -e '
    s/[^,]*,/&\
/4
    tok
    N
    :ok
    s/\n//
' datafile

note: failures will occur when
   a) any line ends with a ',' ,e.g.,
           field1,field2,field3,
           field4,field5
   b) fields spill over more than 2 lines, e.g.,
           field1,field2
           ,field3,field4
           ,field5
   c) there are empty lines or lines with no commas.



Relevant Pages

  • Removing rogue ciarraige returns in data file
    ... I'm having a hard time with a CSV file that has carriage returns in a postal ... are the first carriage return after the 45th field separator." ...
    (comp.unix.shell)
  • .CSV Files
    ... My csv file is delimited by commas and text is delimited by ". ... considers carriage returns and next line characters enclosed in " as ... I am facing a problem when trying to open this file in excel. ...
    (microsoft.public.excel)
  • .CSV Files
    ... My csv file is delimited by commas and text is delimited by ". ... considers carriage returns and next line characters enclosed in " as ... I am facing a problem when trying to open this file in excel. ...
    (microsoft.public.excel.misc)
  • Re: CSV file convert
    ... replace the unrecognized carriage returns with real paragraph marks. ... try converting to a table. ... CSV file, load into Word, select all and then convert text to table. ... My CSV file contains commas to seperate each field but the last field ...
    (microsoft.public.word.docmanagement)
  • Re: problems parsing CSV file
    ... Excel is different again. ... irregularity in the CSV file. ... >My first guess was that there was a comma, quote mark or carriage return ... >of these records and which ones piggyback and which ones don't. ...
    (microsoft.public.access.externaldata)