Добавить переменное число переменных

Acabo de encontrar este hilo como cualquier otro. Me gustaría agregar la solución de cuando ya está al final del archivo, usar 'g' seguido de 'G' forzará una actualización del archivo.

Terminé haciendo un botón de macro para esto en mi programa de terminal (SecureCRT ). La macro es simplemente 'gG'.

0
20.06.2019, 21:42
2 ответа

С массивом данных GNU:

$ datamash -t, --header-in groupby 1 sum 3,4 < file.csv
FTWTRAQNETSQL01,2114,2052.698
FTWTRAQNETSQL02,2114,1993.155
ftwvocmpsqln01,1106,76.426
ftwspsqln02,501,0
ftwvocmpsqln02,1106,76.112
1
28.01.2020, 02:30

Не оболочка, а "unix way":

awk -F',' 'NR==1; NR>1{s3[$1]+=$3; s4[$1]+=$4} END { for(i in s3){printf("%s,%s,%s\n",i,s3[i],s4[i])} }' file

Порядок вывода будет (возможно )не соответствовать порядку ввода.

Описание:

awk                   # use awk.
-F','                 # set the field separator as comma (,)
'                                         # start an awk script.
   NR==1;                                 # print first line (header)
   NR>1{                                  # for lines other than first
         s3[$1]+=$3;                      # add values on third field
         s4[$1]+=$4                       # add values on fourth field
       }                                  # close the previous {
         END {                            # after all lines have been read
               for(i in s3){              # for each index of the array
                                          # (all unique values of field $1)
                             printf("%s,%s,%s\n",i,s3[i],s4[i])   # print values.
                           }              # close the for loop.
             }                            # close the END loop.
' file                                    # end script code and name the file.
1
28.01.2020, 02:30

Теги

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