Search code examples
unixawksolaris

How to get the unique values from column to row


I have a input file like:

> cat test_mfd_1
16,281474976750348
17,281474976750348
16,281474976750348
17,281474976750348
16,281474976749447
17,281474976749447
16,281474976749447
17,281474976749447

And I need the output like:

281474976750348 16,17
281474976749447 16,17

Column 2 and 1 both have duplicated values. But as o/p it should find the unique values in column 2 and print all corresponding unique values as in row.

I am using awk and i get the o/p like below.

awk -F, '{a[$2]=$1;} END {for(i in a) print i" "a[i];}' test_mfd_1
281474976749447 17
281474976750348 17

I am not able to print all unique values from column 1 in front of column 2


Solution

  • Using Perl

    $ cat jeevan.txt
    16,281474976750348
    17,281474976750348
    16,281474976750348
    17,281474976750348
    16,281474976749447
    17,281474976749447
    16,281474976749447
    17,281474976749447
    
    $ perl -F, -lane ' $kv{$F[1]}{$F[0]}++; END { while(my($x,$y) = each(%kv)) { print "$x ",join(",",keys %$y) } }' jeevan.txt
    281474976749447 16,17
    281474976750348 16,17
    

    or

    $ perl -F, -lane ' $kv{$F[1]}{$F[0]}++; END { print "$_ ",join(",",keys %{$kv{$_}}) for(keys %kv) } ' jeevan.txt
    281474976749447 16,17
    281474976750348 16,17
    

    or

    $ perl -F, -lane ' push @{$kv{$F[1]}},$F[0]; END { for(keys %kv) { %p=map{ $_ => 1} @{$kv{$_}} ; print "$_ ",join(",", keys %p) } } ' jeevan.txt
    281474976749447 17,16
    281474976750348 16,17
    

    or

    $ perl -F, -lane ' push @{$kv{$F[1]}},$F[0]; END { for my $a (keys %kv) { @p=grep{ !$s{$a}{$_}++ } @{$kv{$a}} ; print "$a ",join(",", @p) } } ' jeevan.txt
    281474976749447 16,17
    281474976750348 16,17
    

    or

    $ perl -F, -lane ' push @{$kv{$F[1]}},$F[0]; END { for my $a (keys %kv) { print "$a ",join(",", grep{ !$s{$a}{$_}++ } @{$kv{$a}}) } } ' jeevan.txt
    281474976750348 16,17
    281474976749447 16,17
    

    Since this resembles SQL, you can use sqlite also

    $ cat ./sqllite_unique.sh
    #!/bin/sh
    sqlite3 << EOF
    create table t1(a,b);
    .separator ','
    .import $1 t1
    select b|| ' ' || group_concat(distinct a) from t1 group by b;
    EOF
    
    $ ./sqllite_unique.sh jeevan.txt
    281474976749447 16,17
    281474976750348 16,17