将跟踪代码添加到CSV文件每行中链接的第一个实例中



我想使用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

使用文本::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;

最新更新