Finding max value of a specific date awk

后端 未结 3 649
天涯浪人
天涯浪人 2021-01-29 14:18

I have a file with several rows and with each row containing the following data-

name 20150801|1 20150802|4  20150803|6  20150804|7  20150805|7  20150806|8  2015         


        
3条回答
  •  猫巷女王i
    2021-01-29 14:30

    I think you mean this:

    awk -v date=20150823 '{for(f=2;f<=NF;f++){split($f,a,"|");if(a[1]==date&&a[2]>max){max=a[2];name=$1}}}END{print name,max}' YourFile
    

    So, you pass the date you are looking for in as a variable called date. You then iterate through all fields on the line, and split the date and value of each into an array using | as separator - a[1] has the date, a[2] has the value. If the date matches and the value is greater than any previously seen maximum, save this as the new maximum and save the first field from this line for printing at the end.

提交回复
热议问题