Databases

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Splitting a column in a csv file

    6 answers - 612 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    I've tried to find information on this and also tried different csv
    file editors without luck.
    My problem is how can I break a column with a date 01122006 into three
    new columns DAY 01 MNTH 12 YEAR 2006?
    I'd use excels left() and right() formulas to do it in excel, but my
    CSV files are too large for excel and I'd need to do the operation on
    several files.
    Thanks,
    Veka
    a side note, I'm using the csv files in Microsoft Query, so if the
    column split up could be made in the schema.ini file that could also be
    something
  • No.1 | | 928 bytes | |

    In article <1145702739.864322.93800@i40g2000cwc.googlegroups.c om>,
    vfv@iki.fi says
    I've tried to find information on this and also tried different csv
    file editors without luck.

    My problem is how can I break a column with a date 01122006 into three
    new columns DAY 01 MNTH 12 YEAR 2006?

    I'd use excels left() and right() formulas to do it in excel, but my
    CSV files are too large for excel and I'd need to do the operation on
    several files.

    Thanks,

    Veka

    a side note, I'm using the csv files in Microsoft Query, so if the
    column split up could be made in the schema.ini file that could also be
    something

    This would be a job for some scripting language. I would create a REXX
    script to read the csv file, but other scripting languages would work
    also. If you want to use REXX to get this done I can help you with it.
  • No.2 | | 858 bytes | |

    Veka wrote:
    I've tried to find information on this and also tried different csv
    file editors without luck.

    My problem is how can I break a column with a date 01122006 into three
    new columns DAY 01 MNTH 12 YEAR 2006?

    I'd use excels left() and right() formulas to do it in excel, but my
    CSV files are too large for excel and I'd need to do the operation on
    several files.

    A good job for perl. If you only have that single column, you ca do it
    with this single line of perl script:

    perl -pe 's/(\d{2})(\d{2})(\d{4})/DAY $1 MNTH $2 YEAR $3/'

    If you have more columns, it will be nessecary to make sure that they
    aren't matched by the above regular expression. You can post the columns
    here and I can return the snipplet that will do the job.

    Regards,

    Michael.
  • No.3 | | 621 bytes | |

    Hi Michael,

    It would be great to get a fix on this. I have a bunch of CSV files in
    a folder. They all have column headings like:

    FIRSTNAMELASTNAMEPRDUCTEMAILPERIDINFINDICATR_VALUE
    ADAMAU11111100AU@email.se200601ALAS_IN10.000000

    I'd need the fifth column i.e. PERID to be created into two new
    columns YEAR and MNTH.

    I haven't used perl, but if I understand it correctly I just need to
    download something like activeperl and insert the code and then it'll
    be an executable. I'm much more familiar with say visualbasic.

    Thanka,
    Veka

  • No.4 | | 249 bytes | |

    Thanks a lot. Too a while to figure how to run perl programs. I run the
    program. It creates a new file the size of 0kb and gives the following
    error message:Can't emulate -e on #! line at
    c:\perl\perlscripts\file.csv
  • No.5 | | 1293 bytes | |

    Veka wrote:
    Hi Michael,

    It would be great to get a fix on this. I have a bunch of CSV files in
    a folder. They all have column headings like:

    FIRSTNAMELASTNAMEPRDUCTEMAILPERIDINFINDICATR_VALUE
    ADAMAU11111100AU@email.se200601ALAS_IN10.000000

    I'd need the fifth column i.e. PERID to be created into two new
    columns YEAR and MNTH.

    I haven't used perl, but if I understand it correctly I just need to
    download something like activeperl and insert the code and then it'll
    be an executable. I'm much more familiar with say visualbasic.

    Yes. Just download ActivePerl and install it. a command prompt and
    paste this code on one line:

    perl -ane '$F[4] =~ s/(\d{4})(\d{2})/$1 $2/; print join("\t", @F), "\n"'
    <myoldfile.csv >mynewfile.csv

    Remember to be careful that it is written exactly as above.

    You could also put it in a file with the extension pl, which should make
    it executable on Windows. In that case, the script becomes

    #!/usr/bin/perl -ane
    $F[4] =~ s/(\d{4})(\d{2})/$1 $2/;
    print join("\t", @F), "\n";

    Just run it from the command prompt like this:

    myscript.pl <myoldfile.csv >mynewfile.csv

    Good luck.

    Regards,

    Michael.
  • No.6 | | 408 bytes | |

    Veka wrote:
    Thanks a lot. Too a while to figure how to run perl programs. I run the
    program. It creates a new file the size of 0kb and gives the following
    error message:Can't emulate -e on #! line at
    c:\perl\perlscripts\file.csv

    Yes. Replace

    #!/usr/binPerl -ane

    With

    #!/usr/binPerl -an

    (In other words, jst delete the "e".)

    Regards,

    Michael.

Re: Splitting a column in a csv file


max 4000 letters.
Your nickname that display:
In order to stop the spam: 2 + 1 =
QUESTION ON "Databases"

EMSDN.COM