parse-csv.rex

Pipelines v2.1

 

.....|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8....+....9....+....10...

Home

     **** Top of file ****
   1
   2 /* The following pipeline handles two input arguments:
   3
   4    arg 1. The CSV field delimiter.
   5    arg 2. The character which will replace the CSV delimiter.
   6
   7    The following pipeline replaces the CSV field delimiter 'curdelim' in each input record with the character
   8    specified in: 'newdelim'. The modified records are written back to the caller; who can then
   9    operate on them, in terms of words and fields.
  10
  11    Note: the caller must restore the original CSV delimiter character if the records are
  12    to be written to a CSV file. */
  13
  14 Address Rxpipe
  15
  16 Parse Arg curdelim newdelim
  17
  18 'pipe (stagesep ! reg 3)',
  19      'in',                          /* Read records from the caller. */
  20      '! specs 1-* 1 xfefe n',       /* Tag the end of the record. */
  21         ,
  22         /* Parse/split the record into its individual fields. */,
  23      '! split before reg /(\s"\s([^"]*|"{2})*"('curdelim'|$))|"[^"]*"('curdelim'|$)|' || ,
  24              '[^'curdelim']+('curdelim'|$)|('curdelim')/',
  25      '! specs 1;-2 1 /'newdelim'/ n',  /* Tag the end of the field with the new field delimiter. */
  26      '! split before str xfe',      /* Split off the end of record tag, and join.. */
  27      '! join until 1 xfe',          /* ..the fields back together. */
  28      '! out'                        /* Write records back to the caller. */
  29
  30 Exit 0
     **** End of file ****
 

 

 
Validate the arguments!