1

我想使用 perl 在 csv 文件的每一行中的第一个链接实例中添加一个跟踪代码。我怎么能那样做?

例子

输入:

ID, text1, link1, text2, text3, link2
1234, something, http://www.example.com/a/b/c, "lorem", "ipsum", http://www.example.com/image.gif
1235, something, http://www.example.com/dddd, "lorem", "ipsum", http://www.example.com/image.gif
1236, something, http://www.example.com/e/f/g/h, "lorem", "ipsum", http://www.example.com/image.gif

输出:

ID, text1, link1, text2, text3, link2
1234, something, http://www.example.com/a/b/c?tracking_code=1&tr2=2, "lorem", "ipsum", http://www.example.com/image.gif
1235, something, http://www.example.com/dddd?tracking_code=1&tr2=2, "lorem", "ipsum", http://www.example.com/image.gif
1236, something, http://www.example.com/e/f/g/h?tracking_code=1&tr2=2, "lorem", "ipsum", http://www.example.com/image.gif
4

1 回答 1

3

使用Text::CSV正确处理 CSV 文件:

#!/usr/bin/perl
use warnings;
use strict;

use Text::CSV;

my $file = 'file.csv';

my $csv = 'Text::CSV'->new({allow_whitespace => 1,
                            eol => $/,
                           }) or die 'Text::CSV'->error_diag;
open my $CSV, '<', $file or die $!;

my $header = $csv->getline($CSV);
while (my $row = $csv->getline($CSV)) {
    $row->[2] .= '?tracking_code=1&tr=2';
    $csv->print(*STDOUT{IO}, $row);
}
$csv->eof or $csv->error_diag;
于 2013-08-20T18:33:39.353 回答