Add option --separator

This commit is contained in:
Peter J. Holzer 2023-11-06 16:59:00 +01:00
parent 1efa5739d9
commit 3aee99f7f6
1 changed files with 14 additions and 0 deletions

View File

@ -55,6 +55,7 @@ my $vertical; # use vertical output format
my $escape; # escape non-printable characters my $escape; # escape non-printable characters
my $xhtml; # produce XHTML output my $xhtml; # produce XHTML output
my $style; # produce XHTML output my $style; # produce XHTML output
my $separator; # if set, produce CSV with this separator and double-quoted fields
GetOptions( GetOptions(
'help|?' => \$help, 'help|?' => \$help,
@ -62,6 +63,7 @@ GetOptions(
'xhtml' => \$xhtml, 'xhtml' => \$xhtml,
'escape' => \$escape, 'escape' => \$escape,
'style:s' => \$style, 'style:s' => \$style,
'separator:s' => \$separator,
) || pod2usage(2); ) || pod2usage(2);
pod2usage(1) if $help; pod2usage(1) if $help;
@ -163,6 +165,18 @@ if ($xhtml) {
} }
print "\n"; print "\n";
} }
} elsif ($separator) {
no warnings 'uninitialized';
print join($separator, @{$sth->{NAME}}), "\n";
while (my @a = $sth->fetchrow_array()) {
for (@a) {
if (/[$separator"]/) {
s/"/""/g;
s/.*/"$&"/;
}
}
print join($separator, @a), "\n";
}
} else { } else {
no warnings 'uninitialized'; no warnings 'uninitialized';
print join("\t", @{$sth->{NAME}}), "\n"; print join("\t", @{$sth->{NAME}}), "\n";