How to get unique values ​​from a list of duplicate values

I need to parse a large log file (flat file) that contains two columns of values ​​(column-A, column-B).

The values ​​in both columns are repeated. I need to find for each unique value in column-A, I need to find a set of values ​​for column-B.

Is it possible to do this with the unix shell command or write any perl or python script? How can this be done?

Example:

xxxA 2
xxxA 1
xxxB 2
XXXC 3
XXXA 3
xxxD 4

Output:

xxxA - 2,1,3
xxxB - 2
xxxC - 3
xxxD - 4
+3
source share
7 answers

I would use Python dictionaries, where the dictionary keys are the values ​​of column A, and the dictionary values ​​are the built-in Python Set the type by holding the values ​​of column B

def parse_the_file():
    lower = str.lower
    split = str.split
    with open('f.txt') as f:
        d = {}
        lines = f.read().split('\n')
        for A,B in [split(l) for l in lines]:
            try:
                d[lower(A)].add(B)
            except KeyError:
                d[lower(A)] = set(B)

        for a in d:
            print "%s - %s" % (a,",".join(list(d[a])))

if __name__ == "__main__":
    parse_the_file()

, A. , B.

:

  • try-catch , if\ .
  • str , .
  • A A a = lower(A) try catch
  • , Python, .

:

xxxd - 4
xxxa - 1,3,2
xxxb - 2
xxxc - 3
+3

Perl 'one-liner' /, :

$ perl -F -lane '

      $hash{ $F[0] }{ $F[1] }++;
  } END {

      for my $columnA ( keys %hash ) {

          print $columnA, " - ", join( ",", keys %$hash{$columnA} ), "\n";
      }
  '

, .

+5

:

class MultiMap(object):
    values = {}

    def __getitem__(self, index):
        return self.values[index]
    def __setitem__(self, index, value):
        if not self.values.has_key(index):
            self.values[index] = []
        self.values[index].append(value)
    def __repr__(self):
        return repr(self.values)

: http://codepad.org/xOOrlbnf

+1

Perl:

#!/usr/bin/perl

use strict;
use warnings;

my (%v, @row);

foreach (<DATA>) {
        chomp;
        $_ = lc($_);
        @row = split(/\s+/, $_);
        push( @{ $v{$row[0]} }, $row[1]);
} 

foreach (sort keys %v) {
        print "$_ - ", join( ", ", @{ $v{$_} } ), "\n";
}

__DATA__
xxxA 2
xxxA 1
xxxB 2
XXXC 3
XXXA 3
xxxD 4

. , .

+1

f = """xxxA 2
xxxA 1
xxxB 2
XXXC 3
XXXA 3
xxxD 4"""


d = {}

for line in f.split("\n"):
    key, val = line.lower().split()
    try:
        d[key].append(val)        
    except KeyError:
        d[key] = [val]


print d

Python

0

while() {

($key, $value) = split / /, $_;

$hash{lc($key)} = 1;

push(@array, "$key$value");

}

foreach $key (sort keys %hash) {

@arr = (grep /$key/i, @array);

chomp(@arr);

$val = join (", ", @arr);

$val =~ s#$key##gi; 

print "$key\t$val\n";

}
0

Perl oneliner:

perl -lane'$F[0]=~s/.../lc$&/e;exists$s{$F[0]}and$s{$F[0]}.=",$F[1]"or push@v,$F[0]and$s{$F[0]}=$F[1]}{print"$_ $s{$_}"for@v'

$F[0]=~s/.../lc$&/e;, ( ) $F[0]=lc$F[0]; $F[0]=uc$F[0];, .

0

Source: https://habr.com/ru/post/1791644/


All Articles