对多列数据进行排序

对多列数据进行排序

输入(未排序,坏)

"XYZ-ZTE-43255 serverB618 agreed","XYZ-ZTE-44432"
,"XYZ-ZTE-43255"
"XYZ-ZTE-52775 serverB110 agreed",
"XYZ-ZTE-79213 - serverB688 agreed",
"XYZ-ZTE-77323 serverB617 agreed",
"XYZ-ZTE-81422 - serverB609 agreed","XYZ-ZTE-77323"
"XYZ-ZTE-32785 - serverA626 agreed","XYZ-ZTE-52775"
"XYZ-ZTE-43235 - serverA605 disagreed (asdfjlasdj yxvv il lkyeas sadfa)","XYZ-ZTE-43235"
"XYZ-ZTE-11591 serverB144 agreed",
,"XYZ-ZTE-11591"

输出(已排序,需要的东西,好的东西!)

"XYZ-ZTE-43255 serverB618 agreed","XYZ-ZTE-43255"
,"XYZ-ZTE-44432"
"XYZ-ZTE-52775 serverB110 agreed","XYZ-ZTE-52775"
"XYZ-ZTE-79213 - serverB688 agreed",
"XYZ-ZTE-77323 serverB617 agreed","XYZ-ZTE-77323"
"XYZ-ZTE-81422 - serverB609 agreed",
"XYZ-ZTE-32785 - serverA626 agreed",
"XYZ-ZTE-43235 - serverA605 disagreed (asdfjlasdj yxvv il lkyeas sadfa)","XYZ-ZTE-43235"
"XYZ-ZTE-11591 serverB144 agreed","XYZ-ZTE-11591"

简而言之:这是一个包含两列的 XLS 的片段。两列必须按以下方式排序在一起:

左列中的 XYZ-ZTE-77323 与右列中的 XYZ-ZTE-77323 匹配

但也有这样的事情:

,"XYZ-ZTE-43255"

这必须与以下行一致:

"XYZ-ZTE-43255 serverB618 agreed","XYZ-ZTE-43255"

但该行(在输入中)已经包含右列:

"XYZ-ZTE-43255 serverB618 agreed","XYZ-ZTE-44432"

例如,如果:

"XYZ-ZTE-44432"

左列中不存在,则它必须位于 OUTPUT 中的新行中

有人知道如何做到这一点吗?

答案1

我尝试了这个程序,不是最好的程序(它解析一个文件两次并有一些重复的代码),但请随意调整它以满足您的需要。我认为它确实有效。

$ cat script.pl
use warnings;
use strict;
use Text::CSV_XS;

my (%col1, %col2);

my $csv = Text::CSV_XS->new(
        { empty_is_undef => 1 }
) or die "Error: " . Text::CSV_XS->error_diag();

chomp( my @data = <STDIN> );

## Read file and save first column in %col1 hash and second
## column in %col2 hash.
foreach my $line ( @data ) {
    die "Error in parse of CSV file\n" unless $csv->parse( $line );
    my @columns = $csv->fields();
    $col1{ $columns[0] }++ if defined $columns[0];
    $col2{ $columns[1] }++ if defined $columns[1];
}

LINE:
foreach my $line ( @data ) {
        die "Error in parse of CSV file\n" unless $csv->parse( $line );
        my @columns = $csv->fields();

        ## Discard line if both columns are undefined.
        next if !defined $columns[0] && !defined $columns[1];

        ## 1.- Undefined first column: Save second column in hash.
        do { $col2{ $columns[1] } = 1; next } unless defined $columns[0];

        ## 2.- Both columns are defined: Sort them.
        if ( defined $columns[0] && defined $columns[1] ) {
                if ( index( $columns[0], $columns[1] ) > -1 ) {
                        # Line is sorted, print it.
                        print quote($columns[0]), ",", quote($columns[1]), "\n";
                        delete $col2{ $columns[1] };
                } else {
                        # Line unsorted, search its equivalent in hash of second column
                        # and print.
                        my $key = $1 if $columns[0] =~ /^(\S*)/;
                        print quote($columns[0]), ",", ( exists $col2{ $key } ? quote($key) : "" ), "\n";
                        delete $col2{ $key } if exists $col2{ $key };
                        # Here, the second unsorted column, search its equivalent in first
                        # column. If not found print it now, else it will be printed later.
                        for my $str ( keys %col1 ) {
                                next LINE if index( $str, $columns[1] ) > -1;
                        }
                        print ",", quote($columns[1]), "\n";
                }
                next;
        }

        ## 3.- Undefined second column: Check if second column is saved in
        ## hash and join it with first column.
        unless ( defined $columns[1] ) {
                my $key = $1 if $columns[0] =~ /^(\S*)/;
                print quote($columns[0]), ",", ( exists $col2{ $key } ? quote($key) : "" ), "\n";
                delete $col2{ $key } if exists $col2{ $key };
        }
}

sub quote {
        my ($str) = $_[0];
        $str =~ s/^(.*)$/"$1"/;
        return $str;
}

您的数据文件:

"XYZ-ZTE-43255 serverB618 agreed","XYZ-ZTE-44432"
,"XYZ-ZTE-43255"
"XYZ-ZTE-52775 serverB110 agreed",
"XYZ-ZTE-79213 - serverB688 agreed",
"XYZ-ZTE-77323 serverB617 agreed",
"XYZ-ZTE-81422 - serverB609 agreed","XYZ-ZTE-77323"
"XYZ-ZTE-32785 - serverA626 agreed","XYZ-ZTE-52775"
"XYZ-ZTE-43235 - serverA605 disagreed (asdfjlasdj yxvv il lkyeas sadfa)","XYZ-ZTE-43235"
"XYZ-ZTE-11591 serverB144 agreed",
,"XYZ-ZTE-11591"

结果:

$ perl script.pl <yourdatafile
"XYZ-ZTE-43255 serverB618 agreed","XYZ-ZTE-43255"
,"XYZ-ZTE-44432"
"XYZ-ZTE-52775 serverB110 agreed","XYZ-ZTE-52775"
"XYZ-ZTE-79213 - serverB688 agreed",
"XYZ-ZTE-77323 serverB617 agreed","XYZ-ZTE-77323"
"XYZ-ZTE-81422 - serverB609 agreed",
"XYZ-ZTE-32785 - serverA626 agreed",
"XYZ-ZTE-43235 - serverA605 disagreed (asdfjlasdj yxvv il lkyeas sadfa)","XYZ-ZTE-43235"
"XYZ-ZTE-11591 serverB144 agreed","XYZ-ZTE-11591"

答案2

将数据填充到数据库中,然后使用联接根据您喜欢的任何条件检索数据。 SQLite 是一个很好的轻量级系统,没有服务器组件,非常适合以这种方式操作小型数据集。

答案3

这会使用 python 产生您预期的输出。这有点 hackish,因为我不太了解 Pyton,但它确实有效:)

#!/bin/bash

python -c '
import sys, csv;
reader = csv.reader(sys.stdin)
nbli=0 # line number (to maintin input order)
nbwi=6 # width of line number.. (zero padded) 
left=[] 
rght=[] 
for row in reader:
    nbli+=1
    fnum=format(nbli, "0"+str(nbwi)+"d")
    if row[0] != "": left.append(row[0]+fnum) 
    if row[1] != "": rght.append(row[1]+fnum)
left.sort()
rght.sort()
coll = []
l ,r = 0, 0
while l < len(left) or r < len(rght):
    #
    if l >= len(left):
        numr = rght[r][-nbwi:]
        datr = rght[r][:len(rght[r])-nbwi]
        coll.append(numr + " " + ",\"" + datr + "\"")
        r+=1
    elif r >= len(rght):
        numl = left[l][-nbwi:]
        datl = left[l][:len(left[l])-nbwi]
        coll.append(numl + " " + "\"" + datl + "\",")
        l+=1
    else:
        numl = left[l][-nbwi:]
        numr = rght[r][-nbwi:]
        datl = left[l][:len(left[l])-nbwi]
        datr = rght[r][:len(rght[r])-nbwi]
        #
        if datl.startswith(datr+" "):
            coll.append(numl + " " + "\"" + datl + "\",\"" + datr + "\"")
            l+=1
            r+=1
        elif datl < datr + " ":
            coll.append(numl + " " + "\"" + datl + "\",")
            l+=1
        else:
            coll.append(numr + " " + ",\"" + datr + "\"")
            r+=1

coll.sort()
c = 0
while c < len(coll):
    print coll[c][nbwi+1:]
    c+=1
' \
<<-'STDIN'
"XYZ-ZTE-43255 serverB618 agreed","XYZ-ZTE-44432"
,"XYZ-ZTE-43255"
"XYZ-ZTE-52775 serverB110 agreed",
"XYZ-ZTE-79213 - serverB688 agreed",
"XYZ-ZTE-77323 serverB617 agreed",
"XYZ-ZTE-81422 - serverB609 agreed","XYZ-ZTE-77323"
"XYZ-ZTE-32785 - serverA626 agreed","XYZ-ZTE-52775"
"XYZ-ZTE-43235 - serverA605 disagreed (asdfjlasdj yxvv il lkyeas sadfa)","XYZ-ZTE-43235"
"XYZ-ZTE-11591 serverB144 agreed",
,"XYZ-ZTE-11591"
STDIN

输出:

"XYZ-ZTE-43255 serverB618 agreed","XYZ-ZTE-43255"
,"XYZ-ZTE-44432"
"XYZ-ZTE-52775 serverB110 agreed","XYZ-ZTE-52775"
"XYZ-ZTE-79213 - serverB688 agreed",
"XYZ-ZTE-77323 serverB617 agreed","XYZ-ZTE-77323"
"XYZ-ZTE-81422 - serverB609 agreed",
"XYZ-ZTE-32785 - serverA626 agreed",
"XYZ-ZTE-43235 - serverA605 disagreed (asdfjlasdj yxvv il lkyeas sadfa)","XYZ-ZTE-43235"
"XYZ-ZTE-11591 serverB144 agreed","XYZ-ZTE-11591"

相关内容