首页 > 解决方案 > 在shell中,获取一列所有行中的最大值并与每一行进行比较

问题描述

我需要在特定列(C2)的所有行中获取最大值并将值保存在变量中,比如说“max”。然后将 max 变量值与每一行进行比较,如果行值大于或等于 max column3(c3) 应该打印通过,如果行值小于最大值,C3 应该打印失败。我写了下面的代码,但它给了我全部通过。我的脚本有什么问题?

cat /tmp/test.csv
awk 'BEGIN{ max=0 } {if(($2).max) max=($2)}END {print $1,$2,max}' /tmp/test.csv
cat /tmp/test.csv
awk -F '' 'BEGIN{ OFS=";"; print "sep=;\nPackages;count;maxValue;Validation;'};
{if($2 >= $3)
print $1,$2,$3,"passed"
else
print $1,$2,$3,"failed";}'/tmp/test.csv

my csv file looks:
Vmname   Packages   count   
-----------------
Vm1      a,b,c,d     4
vm2      a,b,c       3
vm3      a,b         2


my expected output is:
   vmname    Packages   count   maxValue  Validation 
    --------------------------------------
    vm1       a,b,c,d     4       4        passed
    vm2       a,b,c       3       4        failed
    vm3       a,b         2       4        failed

标签: bashshell

解决方案


编辑:自从 OP 提到 Input_file 以来添加解决方案,它仅与前一个略有不同。

awk '
FNR==NR{
  if(FNR==1 || FNR==2){ next }
  num=split($0,aray,",")
  max=max>num?max:num
  next
}
FNR==1{
  print $0,"maximum Validation"
  next
}
FNR==2{
  print
  next
}
{
  num=split($0,array,",")
}
{
  print $0,max,max==num?"Passed":"Failed"
}
'  Input_file  Input_file | column -t

说明:为上述添加详细说明。

awk '                                         ##Starting awk program from here.
FNR==NR{                                      ##Checking condition FNR==NR which will be TRUE when first time Input_file is being read.
  if(FNR==1 || FNR==2){ next }                ##Checking if line is 1st or 2nd line then simple skip that line.
  num=split($0,aray,",")                      ##Splitting current line into array named aray with delimiter comma here. Where num will be total nuber of elements in array here.
  max=max>num?max:num                         ##Checking if max is greater than num then keep max value or assign num value to max.
  next                                        ##next will skip all further statements from here onwards.
}
FNR==1{                                       ##Checking condition if FNR==1 its first line then do following.
  print $0,"maximum Validation"               ##Printing current line with maximum and Validation sting here in very first line of Input_file.
  next                                        ##next will skip all further statements from here onwards.
}
FNR==2{                                       ##Checking condition if FNR==2 then do following.
  print                                       ##Printing current line here.
  next                                        ##next will skip all further statements from here onwards.
}
{
  num=split($0,array,",")                     ##Splitting current line into array with delimiter comma and total number of array will be stored into variable num here.
}
{
  print $0,max,max==num?"Passed":"Failed"     ##Printing current line then max and printing passed or failed as per condition here.
}
'  Input_file  Input_file | column -t         ##Mentioning Input_file names here and sending awk program output to column command to arrange it better.


您能否尝试以下操作,我认为您的 Input_file 如下:

cat Input_file
Packages
-------
a,b,c,d
a,b,c
a,b

那么以下是解决方案:

awk '
FNR==NR{
  if(FNR==1 || FNR==2){ next }
  num=split($0,aray,",")
  max=max>num?max:num
  next
}
FNR==1{
  print $0,"count maximum Validation"
  next
}
FNR==2{
  print
  next
}
{
  num=split($0,array,",")
}
{
  print $0,num,max,max==num?"Passed":"Failed"
}
'  Input_file Input_file | column -t

推荐阅读