On 02/06/10 11:50, Jan-Benedict Glaw wrote:
> On Tue, 2010-06-01 22:16:14 +0100, A. Dreyer (LUG-OWL) <[email protected]> 
> wrote:
>> On 01/06/10 13:19, Jan-Benedict Glaw wrote:
>>> Mit dem IFS hatte ich schon gespielt; das machts aber nicht unbedingt
>>> einfacher, weil dann z.B. kein Komma mehr in einem der C-Strings
>>> auftauchen darf. Ebenso würden Geschichten wie
>>>
>>>     foo ("This is a text with a real (\") quotation mark", 5.3);
>>>
>>> nicht mehr ordentlich geparst werden.
>>
>> Ich denke für eine derartige Aufgabe ist die Shell eigentlich das
>> falsche Tool und ich würde hier auf Perl und zB. Text::CSV zurück greifen..
> 
> Wie siehts da mit dem Escape-Handling aus? (/me hat keine Ahnung von
> Perl.)
> 
> MfG, JBG

Das Escape-Handling würde ich hier dem Text::CSV module überlassen.
Abhängig vom Format der Rohdaten:

---------------------------------------------------------------------

$ cat test-data1.txt
foo ("This is a text with a real (\") quotation mark", 5.3);

::::::::::::::
test-function.pl
::::::::::::::
#!env perl

use strict;
use warnings;
use diagnostics;
use Text::CSV;

my $file = "test-data1.txt";
$file = $ARGV[0] if $ARGV[0];

my %csvparm = (
        binary           => 1,
        sep_char         => ',',
        quote_char       => '"',
        escape_char      => "\\",
        empty_is_undef   => 1,
        allow_whitespace => 1,
        eol => $/,
);
my $csv = Text::CSV->new (\%csvparm);

open my $io, "<", $file or die "$file: $!";
while (<$io>) {
        my $line = $_;
        $line =~ s/^[^\(]*\(//g;
        $line =~ s/\)[^\)]*$//g;

        my $status = $csv->parse($line);
        next if !$status;

        my @fields = $csv->fields();
        for (@fields) {
                print $_ . "\n" if defined($_);
        }
}
$csv->eof or $csv->error_diag();
close $io;
exit 0;

---------------------------------------------------------------------

$ cat test-data2.txt
"This is a text with a real (\") quotation mark", 5.3

::::::::::::::
test-param.pl
::::::::::::::
#!env perl

use strict;
use warnings;
use diagnostics;
use Text::CSV;

my $file = "test-data.txt";
$file = $ARGV[0] if $ARGV[0];

my %csvparm = (
        binary           => 1,
        sep_char         => ',',
        quote_char       => '"',
        escape_char      => "\\",
        # allow_loose_escapes => 1,
        empty_is_undef   => 1,
        allow_whitespace => 1,
        eol => $/,
);
my $csv = Text::CSV->new (\%csvparm);

open my $io, "<", $file or die "$file: $!";
while (my $row = $csv->getline ($io)) {
        my @fields = @$row;
        for (@fields) {
                print $_ . "\n";
        }
}
$csv->eof or $csv->error_diag();
close $io;
exit 0;

---------------------------------------------------------------------




Regards,
Achim Dreyer
-- 
Achim Dreyer                || http://www.adreyer.com/
Network Security Consultant || Phone: +44 7756948229
Senior Unix & Network Admin || RHCE, RHCA, CCNA, CCSA, CCSE, CCSE+, CSCE
CAcert Assurer              || JNCIS-FW
--
Linux mailing list [email protected]
subscribe/unsubscribe: http://lug-owl.de/mailman/listinfo/linux
Hinweise zur Nutzung: http://www.lug-owl.de/Mailingliste/hints.epo

Antwort per Email an