bash - 在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
解决方案
编辑:自从 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
推荐阅读
- mysql - Docker:在停止的容器中编辑“my.cnf”文件
- node.js - 无法获取某些电子邮件的 Internet 邮件标头
- cmd - MSDeploy -setParam 选项将连接字符串中的 `"""` 替换为`"""`
- javascript - 如何使用 jsreport 和 jsreport Chrome Pdf 渲染 1k pdf 文件?
- python - 如何通过引用 numpy 数组的坐标来处理奇怪的索引行为?
- angular - LoaderController Ionic 4 从组件或页面中隐藏
- java - SurfaceTexture Android Camera2
- scala - Spark Dataframes:将条件列添加到数据框
- ios - Firebase 控制台在验证 iOS 应用上的安装时卡住
- javascript - 这可以在'node'和'hls.js'中使用'stream'模块吗?#2216