Re: [OT] Bash Script Help

From: Colin Ingram (synergizedmusic_at_gmail.com)
Date: 05/24/05

  • Next message: Colin Ingram: "Re: [OT] Bash Script Help"
    Date: Mon, 23 May 2005 21:39:02 -0500
    To: Paul Smith <psmith@nortel.com>
    
    

    Paul Smith wrote:

    >%% Colin Ingram <synergizedmusic@gmail.com> writes:
    >
    > ci> This is not a debian specific question but I thought some of you
    > ci> could help. I am writing a shell script to parse a CSV file
    >
    >Why would you choose bash to do this? The shell is great for running
    >commands, but it's really poor at parsing text, compared to
    >alternatives.
    >
    I was hoping to make this solution as simple as possible, so that my
    colleagues (most won't know perl or any other scripting language, but
    have experience with the shell) may use this code and be able to modify
    it easily to fit their needs. (They may or may not start with a file of
    the same format)

    >The most obvious is Perl, and you can even:
    >
    > # apt-get install libtext-csv-perl
    >
    >to install a Perl module that will parse CSV _for_ you, managing all the
    >quoting, escaping, etc.
    >
    >
    I'll check this out. I guess I can always be available for others in
    case they need help with their modifications.

    >Here's a sample program (note your CSV example is not a valid file: in a
    >real CSV file you can't have whitespace after commas like that;
    >
    I realize that but I can't change the file...our crappy, proprietary
    image processing software generates in that way. Needless to say, the
    software has little documentation and is poorly support; I have know
    idea how to customize the output. (I also have little say in using an
    alternative program, although I'm working hard on convincing the boss
    that change is needed)

    >I added
    >a stupid line to fix this but it breaks things if you have ", " in any
    >quoted field).
    >
    >
    >-------------------------------------------------------------------------------
    >#!/usr/bin/perl
    >
    >use Text::CSV;
    >
    >my $csv = Text::CSV_XS->new();
    >
    >open(CSV, $ARGV[0]) or die "open: $ARGV[0]: $!\n";
    >
    >while (defined ($_ = <CSV>)) {
    > # Valid CSV cannot have whitespace after commas--note this breaks if
    > # you have ", " inside a string... fix your CSV and remove this line!
    > s/, /,/;
    >
    > $csv->parse($_) or warn("invalid CSV line: ", $csv->error_input(), "\n"), next;
    > my @fields = $csv->fields();
    >
    > # Do something with the array... access it as $fields[0] ... $fields[n]
    > print "Fields: (@fields)\n";
    >}
    >
    >close(CSV) or die "close: $ARGV[0]: $!\n";
    >
    >
    >
    >

    -- 
    To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org 
    with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
    

  • Next message: Colin Ingram: "Re: [OT] Bash Script Help"

    Relevant Pages

    • Re: Filling an Excel sheet
      ... write a (shell) script which creates a csv file (comma separated ... read the csv to excel ...
      (comp.unix.shell)
    • Re: PERL Parse CSV
      ... I have a csv file that has some fields containing "," as part of the ... field and not a delimiter. ... shell would see it. ... Paul Lalli ...
      (perl.beginners)
    • Re: Parsing a text file based on command line argument.
      ... and what is d way to call another shell script from ds script... ... Don't use both test and grep. ... You call one shell script from another in the same way as you call ... the checktime is passsed as a command line argument. ...
      (comp.unix.programmer)
    • Re: Parsing a text file based on command line argument.
      ... and what is d way to call another shell script from ds script... ... Don't use both test and grep. ... You call one shell script from another in the same way as you call ... the checktime is passsed as a command line argument. ...
      (comp.unix.programmer)
    • Re: [ANN] Protect you Unix Shell Scripts!
      ... > encodes and encrypts a shell script, ... interpreter, it's no longer platform-independent. ... Most people write platform-independent shell scripts by writing to the ...
      (comp.os.linux.development.apps)