Найдите AWK максимальное значение подряд и печать с заголовком

Вы видите все это для отдельной программы с отладчиком как gdb, но это изменяется так быстро, что Вы не смогли бы видеть, что что-либо смотрит его в прямом эфире, и даже отследить его так Вы видели, что это вообще замедлит компьютер к проверке. Я предлагаю узнать о блоке и компиляторах, это - то, что действительно помогло мне понять такие вещи. Затем можно ступить через программы с gdb если Вы хотите видеть его для реального.

3
03.08.2012, 20:45
2 ответа

вход

$ cat input.txt
Id  sno1  lc1  sno2  lc2    sno3  lc3  sno4 lc4
RM1  98   ss1   88   ms1    78    gs1   45  rs1
RM2  23   ss2   44   ms2    98    gs2   15  rs2
RM3  45   ss3   100  ms3    33    gs3   10  rs3
RM4  45   ss4   45   ms4    12    gs4   11  rs4

сценарий awk

$ cat row_max.awk
NR == 1 {
        for (i = 1; i <= NF; i++) headers[i] = $i;
        next
}

{
        # find maximum value
        max = $2
        for (i = 4; i <= NF; i += 2) if ($i > max) max = $i;
        # print row id
        printf "%s", $1
        # print all lc# column values (assuming the column 
        # after the max value sno# column)
        sep = OFS
        for (i = 2; i <= NF; i += 2) {
                if ($i == max) {
                        printf "%s%s", sep, $(i + 1);
                        sep = ","
                }
        }
        # print all column headers of the max value columns
        sep = OFS
        for (i = 2; i <= NF; i += 2) {
                if ($i == max) {
                        printf "%s%s", sep, headers[i];
                        sep = ","
                }
        }
        printf "\n"
}

вывод

$ awk -f row_max.awk input.txt
RM1 ss1 sno1
RM2 gs2 sno3
RM3 ms3 sno2
RM4 ss4,ms4 sno1,sno2
6
27.01.2020, 21:11
awk 'BEGIN{ getline; for(i=1;i<=NF;i++) hdr[i]=$i; max=-1 }
   { for(i=2; i<=NF; i+=2) 
        if($i==max) { lc=lc","$(i+1); sn=sn","hdr[i] }
        else if($i>max) { max=$i; lc=$(i+1); sn=hdr[i] }
     print $1" "lc" "sn; max=-1 }'  

Здесь это с комментариями

awk 'BEGIN{ 
       # read line 1 column-headers to an array 
       getline; for(i=1;i<=NF;i++) hdr[i]=$i
       # set max to less than zero 
       max=-1
     }
     { # processes lines 2 -> last
       # check each "sn.." field 
       for(i=2; i<=NF; i+=2) 
           if($i==max) { # this cannot occurr for 1st itteration per line  
                # append "lc" field value and "sn" header 
                #  to previous "lc" and "sn", using ","   
                lc=lc","$(i+1); sn=sn","hdr[i] 
            }
            else if($i>max) { # this must occurr on 1st itteration per line 
                # save "lc" field value and "sn" header  
                max=$i; lc=$(i+1); sn=hdr[i]  
            }
       # print output for the current line 
       print $1" "lc" "sn; 
       # set max to less than zero; ready for next line
       # max=-1 
     }

Вывод

RM1 ss1 sno1
RM2 gs2 sno3
RM3 ms3 sno2
RM4 ss4,ms4 sno1,sno2
2
27.01.2020, 21:11
  • 1
    Этот подход, вероятно, более эффективен, чем мой и работает, учитывая текущие предположения. Я просто добавил бы некоторые комментарии для потомства. Я не рассмотрел создающий строку подход. А-ч –  jw013 07.08.2012, 23:27

Теги

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