Comma delimited file problem 
Author Message
 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


Tue, 24 Jul 2001 03:00:00 GMT  
 Comma delimited file problem

Quote:
> I have been given a file which consists of 31 fields. The fields are =
> comma
> delimited and the data is enclosed by "'s.

> =46or example......

> "A","B","C", etc etc etc up to 31 fields

> However, not all fields contain data !!!

> 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.
[...]
> I will print=20
> $2 $1 $17 and $18 and
> $2 $1 $17=20

> So, I need to print $2 $1 $17 and $18 to $31 - but only print $18 to $31 =
> if they
> contain data, and without the "'s

I'll give it a try... (untested code follows)

BEGIN {
        FS = "\",\""
        OFS = " "

Quote:
}

{
#massage first and last fields in standard format; FS allready striped most "'s
        sub(/^"/, "" ,$1)
        sub(/"$/, "", $NF)
#get all data
        data = $17
        for ( i=18; i<=NF; i++ ) {
                if ( $i != "" ) {
                        data = data " " $i
                }
        }
        print $2, $1, data

Quote:
}

--
Peter Roozemaal
... Where's my 640Meg SIMM? I want to run Excel!


Tue, 24 Jul 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Export Clarion .DAT files to ASCII comma delimited files

2. Export Clarion .DAT files to ASCII comma delimited files

3. getting fields NOT comma delimited with commas inside

4. Parsing Comma delimited files in J

5. matching records in a comma delimited file

6. 2.01 Comma delimited ASCII file

7. Import comma delimited text file

8. VW code to read comma-delimited text files??

9. importing from a comma delimited file

10. Comma delimited file?

11. comma delimited file

12. Regular Expressions in Rexx to crack a comma delimited file

 

 
Powered by phpBB® Forum Software