shellsortingawkuniqsolaris-10

Sort by column and uniq by another column


I have below table which is divided with ";" and it is sorted like

Date;Name;ID;Region;Empty;CardNumber;Temperature

I needed to list max temperature of Cards which each card must be unique. Also final list must be sorted greater to smaller by temperature column.

INPUT:

1999;D;153;RIZE;;LT1;86
2013;D;153;RIZE;;LT2;88
2012;D;153;RIZE;;LT3;9
2011;D;153;RIZE;;LT4;81
2010;D;153;RIZE;;LT1;75
1888;D;153;RIZE;;LT2;76
2008;D;153;RIZE;;LT3;77
2006;D;153;RIZE;;LT4;78

OUTPUT:

2013;D;153;RIZE;;LT2;88
1999;D;153;RIZE;;LT1;86
2011;D;153;RIZE;;LT4;81
2008;D;153;RIZE;;LT3;77

I tried below command but gave an irrelevant output. Regards.

sort -t ';' -rnk 7 sil3 | sort -u -t ';' -rk 2,6
2011;D;153;RIZE;;LT4;81
2012;D;153;RIZE;;LT3;9
2013;D;153;RIZE;;LT2;88
2010;D;153;RIZE;;LT1;75

Solution

  • In GNU awk:

    $ gawk '
    BEGIN { FS=OFS=";" }
                          # { k=$2 FS $3 FS $4 FS $6 }
    $7>t[$6] {            # $7>t[k] {
        t[$6]=$7          #     t[k]=$7
        r[$6]=$0          #     r[k]=$0
    }
    END {
        PROCINFO["sorted_in"]="@val_num_desc"
        for(i in t)
            print r[i]
    }' file
    2013;D;153;RIZE;;LT2;88
    1999;D;153;RIZE;;LT1;86
    2011;D;153;RIZE;;LT4;81
    2008;D;153;RIZE;;LT3;77
    

    If you don't have GNU awk available, sort output with sort:

    $ awk '
    BEGIN { FS=OFS=";" }
    $7>t[$6] {
        t[$6]=$7
        r[$6]=$0
    }
    END {
        for(i in t)
            print r[i]
    }' file |
    sort -s -t \; -k7nr 
    2013;D;153;RIZE;;LT2;88
    1999;D;153;RIZE;;LT1;86
    2011;D;153;RIZE;;LT4;81
    2008;D;153;RIZE;;LT3;77