【问题标题】:Sort Multiple line according to fields [closed]根据字段对多行进行排序[关闭]
【发布时间】:2013-07-10 01:33:09
【问题描述】:

我这里有一条多行记录,我要做的是根据类型和 HEADER1 行中的 6 位数字对它们进行排序。

这是记录:

HEADER1|TYPE1|123456|JOHN SMITH
INFO|M|34|SINGLE
INFO|SGT
STATUS|KIA
MSG|NONE
HEADER1|TYPE3|654123|DANICA CLYNE
INFO|F|20|SINGLE
STATUS|MIA
MSG|HELP
MSG1||
HEADER1|TYPE2|987456|NIDALEE LANE
INFO|F|26|MARRIED
STATUS|INJURED
MSG|NONE
HEADER1|TYPE1|123456|JOHN CONNOR
INFO|M|34|SINGLE
STATUS|KIA
MSG|NONE
HEADER1|TYPE4|123789|CAITLYN MIST
INFO|F|19|SINGLE
INFO|||
STATUS|NONE
MSG|NONE
HEADER1|TYPE2|987456|NIDALEE CROSS
INFO|F|26|MARRIED
STATUS|INJURED
MSG|NONE

输出应该是这样的: 它对与规则匹配的行进行排序

HEADER1|TYPE1|123456|JOHN SMITH
INFO|M|34|SINGLE
INFO|SGT
STATUS|KIA
MSG|NONE
HEADER1|TYPE1|123456|JOHN CONNOR
INFO|M|34|SINGLE
STATUS|KIA
MSG|NONE
HEADER1|TYPE2|987456|NIDALEE LANE
INFO|F|26|MARRIED
STATUS|INJURED
MSG|NONE
HEADER1|TYPE2|987456|NIDALEE CROSS
INFO|F|26|MARRIED
STATUS|INJURED
MSG|NONE
HEADER1|TYPE3|654123|DANICA CLYNE
INFO|F|20|SINGLE
STATUS|MIA
MSG|HELP
MSG1||
HEADER1|TYPE4|123789|CAITLYN MIST
INFO|F|19|SINGLE
INFO|||
STATUS|NONE
MSG|NONE

【问题讨论】:

  • 你有没有试过的代码?
  • @kjprice 我还在制定中
  • 数字是否与类型相对应?前任。类型 1 始终具有数字 123456,或者您希望它们先按类型排序,然后再按数字排序?
  • @Matt 我希望它按类型和数字 matt 排序

标签: perl perl-module line-processing


【解决方案1】:

这是我的解决方案。

#!/bin/perl

use warnings;
use strict;

# Read in the file
open(my $fh, '<', "./record.txt") or DIE $!;
my @lines = <$fh>;
my @records;

# populate @records with each element having 4 lines
for ( my $index = 0; $index < scalar @lines; $index+=4 ) {
    push @records, join("", ($lines[$index], $lines[$index+1], $lines[$index+2], $lines[$index+3]));
}

# sort by type and then by numbers
@records =  map { $_->[0] }
            sort { $a->[1] cmp $b->[1] || $a->[2] cmp $b->[2] }
            map { [ $_ , (split('\|', $_))[1], (split('\|', $_))[2] ] }
            @records;

print "@records";

这是一个更新的版本,同样的想法:

#!/bin/perl

use warnings;
use strict;


open(my $fh, '<', "./record.txt") or DIE $!;
my @lines = <$fh>;
my $temp = join ("", @lines);
my @records = split("HEADER1", "$temp");
my @new_records;

for my $rec (@records){
    push @new_records, "HEADER1" . $rec;
}
shift @new_records;



@records =  map { $_->[0] }
            sort { $a->[1] cmp $b->[1] || $a->[2] cmp $b->[2] }
            map { [ $_ , (split('\|', $_))[1], (split('\|', $_))[2] ] }
            @new_records;



print "@records";

【讨论】:

  • 如果 INFO 是两行或 MSG 怎么办?像这样 HEADER1|TYPE1|123456|JOHN SMITH INFO|M|34|SINGLE INFO|UNEMPLOTED STATUS|KIA MSG|NONE MSG1|NONE
  • 您只需要更改读取方式(每 2 行读取一次,而不是循环中的 4 行),但排序仍然相同。你也想按味精排序吗?
  • 不只是类型和数字,我只是想让你知道记录并不总是由 4 行组成
  • 嘿,马特,您更新的解决方案有效 :) 我希望,我能够理解每行的工作原理!
  • 警告 - 对于大文件,这可能会变得非常缓慢:)
【解决方案2】:

如果您不关心性能,并且每个“记录”由 4 行组成:

# Assume STDIN since the question didn't say anything
my $line_index = 0;
my (@records, @record);
# Slurp in all records into array of quadruplets
while (<>) {
    if (0 == $line_index) {
        push @records, [];
    };
    $records[-1]->[$line_index] = $_; # -1 lets you access last element of array.
    $line_index++;
    $line_index = 0 if $line_index == 4; # better done via "%" 
}

# Sort the array. Since we sort by type+id, 
# we can simply sort the first strings alphabetically.
my @records_sorted = sort { $a->[0] cmp $b->[0] } @records;

foreach my $record (@records_sorted) {
    print join("", @$record); # Newlines never stripped, no need to append
}

如果您更喜欢冒险,请使用 List::MoreUtils::natatime:

use List::MoreUtils q/natatime/;
my @lines = File::Slurp::read_file("my_file.txt");
my $it = natatime 4, @lines;
my (@records, @record);
while ((@record) = $it->()) {
    push @records, \@record;
}
my @records_sorted = sort { $a->[0] cmp $b->[0] } @records;
foreach my $record (@records_sorted) {
    print join("", @$record);
}

从@lines 创建@records 的另一个选项是List::Gen

use List::Gen qw/by/; 
foreach my $record (by 4 => @lines) {
    push @records, $record;
}

请注意,上面的代码假定所有#s 都是6 位数字。如果不是这样,你需要稍微修改一下代码:

use List::Gen qw/by/;
my @lines = File::Slurp::read_file("my_file.txt");
my @records;
foreach my $record (by 4 => @lines) {
    my @sort_by = split(m#/#, $record->[0]);
    push @records, [ $record, \@sort_by ];
}
my @records_sorted = sort { 
                             $a->[1]->[1] cmp $b->[1]->[1] 
                          || $a->[1]->[2] <=> $b->[1]->[1]
                     } @records;
foreach my $record (@records_sorted) {
    print join("", @{$record->[0]}); 
}

更新:由于 OP 决定输入文件每条记录可能有任何 # 行,这里是更新的代码:

my (@records, @record);
# Slurp in all records into array of quadruplets
while (<>) {
    if (/HEADER1/) {
        my @sort_by = split(m#/#);            
        push @records, [[], \@sort_by];
    };
    push @{ $records[-1]->[0] }, $_;
}
my @records_sorted = sort { 
                             $a->[1]->[1] cmp $b->[1]->[1] 
                          || $a->[1]->[2] <=> $b->[1]->[1]
                     } @records;
foreach my $record (@records_sorted) {
    print join("", @{$record->[0]}); 
}

【讨论】:

  • 请注意 - 这不是最优雅或最惯用的 Perl 代码。我试着让它对新手更友好。
  • 感谢 DVK,它不必总是 4 行,我应该为此调整什么?
  • @Soncire - 取决于您的文件的外观。我根据你的问题回答了。如果您的文件格式不同,最好作为一个新问题发布,因为在我发布了一个全面的答案后更改输入文件对我来说不太公平:)
  • 哦,我明白了,非常感谢 DVK
【解决方案3】:

使用 List::MoreUtils 'apply' 并将 input_record_separator 设置为 'HEADER',代码可能如下所示。

#!/usr/bin/perl
use strict;
use warnings;
use List::MoreUtils qw/ apply /;

my $fname =  'dup_data.txt';

open (my $input_fh, '<', $fname) or die "Unable to read '$fname' because $!";
open (my $OUTPUTA, ">", $fname .".reformat")
    or die "$0: could not write to '$fname.reformat'. $!";

{
    local $/ = "HEADER";

    print $OUTPUTA map{ "HEADER$_->[0]"}
                  sort {$a->[1] <=> $b->[1] || $a->[2] <=> $b->[2]}
                   map {[$_, /TYPE(\d+)\|(\d+)/]}
                  grep $_, apply {chomp} <$input_fh>;
}
close $input_fh or die $!;
close $OUTPUTA or die $!;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-22
    相关资源
    最近更新 更多