規範化數據
- 使用
Text::CSV
重新排序CSV文件的列。
- 然後,您可以使用Perl ’的s
sort
或其他一些實用程序重新排序文件的行。
這也採用Text::Wrap
在一個可喜的格式顯示標準化文件:
use strict;
use warnings;
use autodie;
# Setup fake data
my @files;
{
local $/ = ''; # Paragraph mode
while (<DATA>) {
chomp;
my ($file, $data) = split "\n", $_, 2;
open my $fh, '>', $file;
print $fh $data, "\n";
push @files, $file;
}
}
# Normalize Files by Column Order
use Text::CSV;
my $csv = Text::CSV->new({ binary => 1, eol => $/ })
or die "Cannot use CSV: " . Text::CSV->error_diag();
for my $file (@files) {
local @ARGV = $file;
local $^I = '.bak';
my @old_order;
my @new_order;
while (<>) {
if (!$csv->parse($_)) {
die "Bad parse $file, line $.: " . $csv->error_diag();
}
my @columns = $csv->fields();
if ($. == 1) {
@old_order = @columns;
@new_order = sort @columns;
}
my %hash;
@hash{@old_order} = @columns;
if (!$csv->combine(@hash{@new_order})) {
die "Bad combine $file, line $.: " . $csv->error_diag();
}
print $csv->string();
}
unlink "$file$^I"; # Optionally delete backup
}
# Normalize Files by Row Order
for my $file (@files) {
my ($header, @data) = do { local @ARGV = $file; <> };
open my $fh, '>', $file;
print $fh $header, sort @data;
}
# View Normalized Files
use Text::Wrap;
for my $file (@files) {
open my $fh, '<', $file;
print wrap(sprintf("%-12s", $file), ' ' x 12, <$fh>,), "\n";
}
__DATA__
file1.csv
NAME,AGE,BDAY
ABC,1,090214
DEF,1,122514
file2.csv
BDAY,NAME,AGE
122514,DEF,1
090214,ABC,1
file3.csv
BDAY,NAME,AGE
122514,DEFG,1
090214,ABC,1
輸出:
file1.csv AGE,BDAY,NAME
1,090214,ABC
1,122514,DEF
file2.csv AGE,BDAY,NAME
1,090214,ABC
1,122514,DEF
file3.csv AGE,BDAY,NAME
1,090214,ABC
1,122514,DEFG
這是一個有點不清楚你問:如何重新排序?如何排序?表明您希望的輸出清楚。提示:'awk'可以通過切換列來完成工作。 – fedorqui 2014-09-02 07:38:41
您可以使用AWK對列進行重新排序,但您需要指定'cat file1 | awk -F',''BEGIN {OFS =「,」;} {print $ 3,$ 1,$ 2}'' – 2014-09-02 07:46:40
@ChrisDoyle是的,好點。但請注意,不需要'cat':'awk'BEGIN {FS = OFS =「,」} {print ...}'file'。 – fedorqui 2014-09-02 07:48:02