【问题标题】:Comparing two csv files matching and merging using perl使用perl比较两个csv文件匹配和合并
【发布时间】:2014-02-21 02:00:03
【问题描述】:

我有两个名为的 csv 文件

alexa_products.csv

name,         sku,      urle,     product,  data

amazon,   amazon.com,   current,  mobile,   seller

vinnes,   vinnes.com,   current,  cellular, Aircel_Indore

数据.csv

name,          sku,      urle,    product,   data

linkedin.com, linkeidn,  current, local,     blah

airtel.com,    airtel,   current, sim,       Airtel

amazon.com,    amazon,   face, network,    buyier

vinnes.com,    vinnes,   look, hands,      ddde

现在我必须匹配来自 alexa_products.csv 的名称和来自 data.csv 的 sku(如果有任何匹配项),我必须仅将来自两个 csv 文件的特定列中的所有数据打印到另一个 csv 文件 ?

预期输出

amazon.com,    amazon,   face, network,    buyier, current,  mobile,   seller

vinnes.com,    vinnes,   look, hands,      ddde,  current,  cellular, Aircel_Indore

【问题讨论】:

  • 请向我们展示预期的输出。
  • @MarkSetchell 您好,我已根据您的要求更新了预期输出

标签: regex perl csv hash iteration


【解决方案1】:

由于您没有提到您感兴趣的列,我只是说当与第一个文件匹配时,此命令将打印第二个文件的所有列。

awk -F, 'FNR==NR && NR!=1 && FNR!=1
        {
        a[$1]=$0;next
        }{if($2 in a)
           {
            split(a[$2],b," ");
            print $0,b[3],b[4],b[5]
           }
         }' alexa_products.csv data.csv

【讨论】:

  • 您好,感谢您的回复,请在主要内容中查看更新后的预期结果
【解决方案2】:

您可以尝试以下方法:

sed "1d;s/ //g" alexa_products.csv | sort > a
sed "1d;s/ //g" data.csv | sort > b
join -t, -1 1 -2 2 a b > newfile.csv

是的,我知道 Perl 不是很好 ;-)

“sed”命令从 alexa_products.csv 中删除标题行(第 1 行)和所有空格。然后使用“sort”对文件的其余部分进行排序并保存为文件“a”。

同样,文件“data_products”的标题和空格被删除,被排序并存储在文件“b”中。

然后 "join" 使用文件 "a" 的字段 1 并将其与文件 b 中的字段 "2" 匹配并打印它们匹配的位置。

您可以使用命令“man sed”或“man join”来阅读有关命令的手册 - 按空格键进入下一页,按“q”退出阅读。

【讨论】:

  • 只需在最后一行的末尾添加 "> newfile.csv"。
  • 但是文件里面什么都没有写?
【解决方案3】:

这里有一些 Perl 可以帮助您入门!

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my %alexa;
my ($name,$sku,$urle,$product,$data);

# Parse first file
my $line=1;
open(my $fh,"<","alexa_products.csv")|| die "ERROR: Could not open alexa_products.csv";
while (<$fh>)
{
   next if $line++==1;  # Ignore header
   chomp;       # Remove LF
   s/ //g;      # Remove spaces
   ($name,$sku,$urle,$product,$data) = split(',');  # Split line on commas
   $alexa{$name}{'sku'}=$sku;
   $alexa{$name}{'urle'}=$urle;
   $alexa{$name}{'product'}=$product;
   $alexa{$name}{'data'}=$data;
}
close($fh);

# Next line for debugging, comment out if necessary
print Dumper \%alexa;

# Now read data file 
$line=1;
open($fh,"<","Data.csv")|| die "ERROR: Could not open Data.csv";
while(<$fh>)
{
   next if $line++==1;      # Ignore header line
   chomp;           # Remove LF
   s/ //g;          # Remove spaces

   my ($name,$sku,$urle,$product,$data) = split(',');   # Split line on commas
   if(defined $alexa{$sku}){
      print "$alexa{$sku}{'sku'},$alexa{$sku}{'data'},$alexa{$sku}{'product'}\n";   # You may want different fields
   }
}

【讨论】:

    猜你喜欢
    • 2012-09-05
    • 2014-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多