Description
We want to extract the N-th record from a comma-separated record where each field may be enclosed with a pair of double quotes.

In the following example, the value of N is 3.
Raw Input Desired Output
0,1,2,"3,4,5",6,7,"8,9,0",1,2
aa,"bb,cc,dd",ee,"ff,gg",hh,ii,jj
A,B,"C,D",E,F,"G,H,I,J",K,L
a,b,,"c,d",e
d,"e,f","g,h,i"
2
ee
"C,D"
 
"g,h,i"
Script and Comments
Script1
[ 1] s/^(([^",]*|"[^"]*"),){2}([^",]*|"[^"]*")(,.*|$)/\3/
Comments
  1. The `-r' option of GNU sed must be used to make sed interpret REs as EREs.
  2. [^",]* matches an unquoted field while "[^"]*" matches a quoted one.
  3. To extract the 3rd field, first we have to write an RE to match the entire record; then discard all except the 3rd field by replacing the entire record with the 3rd field.
  4. To facilitate reading the RE used in Step [1], we divide it into three parts:
    which partregular expressions used
    the first two fields ^(([^",]*|"[^"]*"),){2}
    the 3rd field ([^",]*|"[^"]*")
    the rest of the record(,.*|$)