Re: [OT] Bash Script Help
From: Colin Ingram (synergizedmusic_at_gmail.com)
Date: 05/24/05
- Previous message: Robert Vangel: "Re: my messages to linux.debian.user take 40 minutes to show up"
- In reply to: Paul Smith: "Re: [OT] Bash Script Help"
- Next in thread: Colin Ingram: "Re: [OT] Bash Script Help"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Previous message: Robert Vangel: "Re: my messages to linux.debian.user take 40 minutes to show up"
- In reply to: Paul Smith: "Re: [OT] Bash Script Help"
- Next in thread: Colin Ingram: "Re: [OT] Bash Script Help"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|