Файл сводки AWK из отфильтрованного списка

Следует быть читаемым по умолчанию, убедитесь, что у вас нет странных вариантов монтажа в / etc / fstab или некоторое неуклюжее задача пост-монтажа в etc / init / mounted-proc.conf .

0
04.08.2015, 17:19
1 ответ

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.

0
28.01.2020, 04:56

Теги

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