Sort CSV based on a certain column?

前端 未结 7 957
自闭症患者
自闭症患者 2020-12-18 09:50

I\'m sure I\'ve done this in the past and there is something small I\'m forgetting, but how can I sort a CSV file on a certain column? I\'m interested in answers with and wi

7条回答
  •  情话喂你
    2020-12-18 10:27

    There is also DBD::CSV:

    #!/usr/bin/perl
    
    use strict; use warnings;
    use DBI;
    
    my $dbh = DBI->connect('dbi:CSV:', undef, undef, {
        RaiseError => 1,
        f_ext => '.csv',
        csv_tables => { test => { col_names => [qw' name age sex '] } },
    });
    
    my $sth = $dbh->prepare(q{
        SELECT name, age, sex FROM test ORDER BY age
    });
    
    $sth->execute;
    
    while ( my @row = $sth->fetchrow_array ) {
        print join(',' => @row), "\n";
    }
    
    $sth->finish;
    $dbh->disconnect;
    

    Output:

    name,21,male
    name,24,male
    name,25,female
    name,27,female

提交回复
热议问题