
Comma delimited file problem
Quote:
>Hi all,
>Another dead easy one for the experts, but one that has got me stumped !
>I have been given a file which consists of 31 fields. The fields are comma
>delimited and the data is enclosed by "'s.
Are you sure that none of the fields in double quotes contain commas,
which is the usual reason for putting "" around fields in CSV files.
Assuming fields never have embedded commas, then here are a few
thoughts.
BEGIN {
FS = "," # input field separator
OFS = "," # output field separator (this is normally 1 space)
Quote:
}
# for each record then
{ gsub ( "\"", "", $0 ) } # remove all double quotes
Quote:
>What I have to do is extract fields 1 and 2,swap them round and concatenate them
>together. And then, the only fields that are of interest are 17 and 18 to 31 if
>they contain data. $17 will always contain data.
{ print $2,$1,$17 my_func() }
# make this do whatever you want to $18 to $31
function my_func ( i, result ) {
for ( i = 18 ; i <= 31 ; i++ ) { # check params 18 to 31
if ( $i != "" ) result = result OFS $i
}
return result
Quote:
}
Hope this helps,
--
Steve