Как использовать Unix Shell для отображения только первых n столбцов и последних n столбцов?

try

awk 'NR == FNR { data[$1 " " $2]=0 ; next ; }
{ if ($1 " " $2 in data) data[$1 " " $2]+=$3 }
 END { for ( d in data ) printf "%s %d\n",d,data[d] ;} ' filter data

(это может быть в одной строке)

где

  • NR == FNR { data[$1 " " " $2]=0 ; next ; } store line from filter file
  • { if ($1 " " $2 в данных) data[$1 " " " $2]+=$3 } add value from third colum if in data
    • END { for ( d in data ) printf "%s %d\n",d,data[d] ;} print sum

note that output order is randomized, you can whish to pipe to sort.

первая 3 строка

здесь модифицированный awk

NR == FNR { countit[$1 " " $2]=0 ; next ; }
{ if ($1 " " $2 in countit) {
    data[$1 " " $2]+=$3 ;
    countit[$1 " " $2] ++ ;
    if ( countit[$1 " " $2] == 3 ) {
            printf "%s %s %s\n",$1,$2,data[$1 " " $2] ;
            delete data[$1 " " $2] ;
            delete countit[$1 " " $2] ;
    }
    }
}

 END { for ( d in data ) printf "%s %d\n",d,data[d] ;}

в зависимости от того, что делать с неполным списком (например, 1 или 2 элемента), вы можете удалить строку END.

4
25.01.2018, 03:23
0 ответов

Теги

Похожие вопросы