[英]PERL Script To Compare 3 CSV Files & Create a New CSV File
I'm working to compare 3 csv files and create another csv file with the new output. 我正在比较3个csv文件,并使用新的输出创建另一个csv文件。
File 1 文件1
Name,Color,Food,Point
David,Orange,Pasta,12.34
Raver,Blue,Pizza,13.35
John,Red,Noodles,14.36
Armond,Black,Sandwich,15.37
File 2 文件2
Name,Task David,Success Raver,Success John,Success Armond,Failure
File 3 文件3
Color,Stock Orange,Available Blue,Available Red,OutOfStock Black,Available
Output File 输出文件
Name,Color,Food,Point,Task,Stock
David,Orange,Pasta,12.34,Success,Available
Raver,Blue,Pizza,13.35,Success,Available
John,Red,Noodles,14.36,Success,OutOfStock
Armond,Black,Sandwich,15.37,Failure,Available
Kindly help me. 请帮助我。 Thanks in advance.
提前致谢。
You could use 2 join
s: 您可以使用2个
join
:
join -t, -j 1 file1 file2 | join -t, -1 2 -2 1 -o 1.1,1.2,1.3,1.4,1.5,2.2 - file3
Explanation: 说明:
join -t, -j 1 file1 file2
: joins the first two files at field 1. join -t, -j 1 file1 file2
:在字段1处连接前两个文件。 join -t, -1 2 -2 1 ... - file3
: the previous statement represents now the first file ( -
). join -t, -1 2 -2 1 ... - file3
:前面的语句现在代表第一个文件( -
)。 This is joined with the 3rd file. -o 1.1,1.2,1.3,1.4,1.5,2.2
: The output order as in your example. -o 1.1,1.2,1.3,1.4,1.5,2.2
:示例中的输出顺序。 I have tried something here. 我在这里尝试过一些东西。
Output 产量
Name,Color,Food,Point,Task,Stock
David,Orange,Pasta,12.34,Success,Available
Raver,Blue,Pizza,13.35,Success,Available
John,Red,Noodles,14.36,Success,OutOfStock
Armond,Black,Sandwich,15.37,Failure,Available
This works! 这可行!
#! /ms/dist/perl5/bin/perl5.10
use strict;
my $file1csv = $ARGV[0];
my $file2csv = $ARGV[1];
my $file3csv = $ARGV[2];
open(ff, "<$file1csv");
my @file1_array = <ff>;
close ff;
open(fg, "<$file2csv");
my @file2_array = <fg>;
close fg;
open(f, "<$file3csv");
my @file3_array = <f>;
close f;
open(g, ">Output.csv");
print g "Name,Color,Food,Point,Task,Stock\n";
foreach my $file1_value (@file1_array)
{
$file1_value =~s/^\s+|\s+$|\s//igs;
$file1_value = $file1_value . ",";
print "Processing\n";
if($file1_value =~m/^([^\,]*)\,([^\,]*)\,([\w\W]*)\,$/is)
{
my $name = $1; chomp($name);
my $color = $2; chomp($color);
my $remaining = $3; chomp($remaining);
# my $point = $4; chomp($point);
print "Processing for the name :: $name\n";
my $task;
foreach my $file2_value (@file2_array)
{
$file2_value =~s/^\s+|\s+$|\s//igs;
$file2_value = $file2_value.",";
if($file2_value =~m/$name\,([^\,]*)\,/is)
{
$task = $1; chomp($task);
}
}
my $stock;
foreach my $file3_value (@file3_array)
{
$file3_value =~s/^\s+|\s+$|\s//igs;
$file3_value = $file3_value.",";
if($file3_value =~m/$color\,([^\,]*)\,/is)
{
$stock = $1; chomp($stock);
}
}
open(g, ">>Output.csv");
print g "$name,$color,$remaining,$task,$stock\n";
}
}
close g;
print "Completed\n";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.