App-CSVUtils

 view release on metacpan or  search on metacpan

script/csv2ltsv  view on Meta::CPAN

#!perl

use 5.010001;
use strict;
use warnings;

use Getopt::Long qw(:config gnu_getopt no_ignore_case);
use Text::CSV_XS;

our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2025-02-04'; # DATE
our $DIST = 'App-CSVUtils'; # DIST
our $VERSION = '1.036'; # VERSION

binmode STDIN, ':encoding(utf8)';
binmode STDOUT, ':encoding(utf8)';

my %Opt = (
    header => 1,
);
GetOptions(
    'no-header' => sub { $Opt{header} = 0 },
);

my $csv = Text::CSV_XS->new({binary=>1});
my $i = 0;
my $header_row;
while (my $row = $csv->getline(\*ARGV)) {
    if (++$i == 1) {
        if ($Opt{header}) {
            $header_row = $row;
            for (@$row) { s/:/_/g }
            next;
        } else {
            $header_row = [map { "field".($_+1) } 0..$#{$row}];
        }
    }
    print join("\t", map { "$header_row->[$_]:$row->[$_]" } 0..$#{$row}), "\n";
}

# ABSTRACT: Convert CSV to LTSV
# PODNAME: csv2ltsv

__END__

=pod

=encoding UTF-8

=head1 NAME

csv2ltsv - Convert CSV to LTSV

=head1 VERSION

This document describes version 1.036 of csv2ltsv (from Perl distribution App-CSVUtils), released on 2025-02-04.

=head1 SYNOPSIS

 % csv2ltsv INPUT.csv > OUTPUT.ltsv

=head1 DESCRIPTION

B<Common notes for the utilities:>

Encoding: The utilities in this module/distribution accept and emit UTF8 text.

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/App-CSVUtils>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-App-CSVUtils>.

=head1 SEE ALSO



( run in 0.554 second using v1.01-cache-2.11-cpan-39bf76dae61 )