ParseCSV

Pipelines v2.0

 

/* Example by TenFiftyTwo(c). */

Home

 
/* The following pipeline handles two input arguments:
 
   arg 1. The CSV field delimiter.
   arg 2. The character which will replace the CSV delimiter.
 
   The following pipeline replaces the CSV field delimiters in each input record with the character
   specified in: (arg 2). The modified records are written back to the caller; who can then
   operate on them, in terms of words and fields.
 
   Note: the caller must restore the original CSV delimiter character if the records are
   to be written to a CSV file. */
 
Address Rxpipe
 
Parse Arg curdelim newdelim
 
‘pipe (stagesep ! reg 3)’,
     ‘in’,                          /* Read records from the caller. */
     ‘! specs 1-* 1 xfefe n’,       /* Tag the end of the record. */
      ,
      /* Parse/split the record into its individual fields. */
     ‘! split before reg /(\s"\s([^"]*|"{2})*"('curdelim'|$))|"[^"]*"('curdelim'|$)|' || ,
             '[^'curdelim']+('curdelim'|$)|('curdelim')/',
     ‘! specs 1;-2 1 /’newdelim’/ n’,  /* Tag the end of the field with the new field delimiter. */
     ‘! split before str xfe’,      /* Split off the end of record tag, and join.. */
     ‘! join until 1 xfe’,          /* ..the fields back together. */
     ‘! out’                        /* Write records back to the caller. */
 
Exit 0