Finding max value of a specific date awk

后端 未结 3 648
天涯浪人
天涯浪人 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条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-29 14:44

    You couldn't have taken 5 seconds to give your sample input different values? Anyway, this may work when run against input that actually has different values for the dates:

    $ cat tst.awk
    BEGIN { FS="[|[:space:]]+" }
    FNR==1 {
        for (i=2;i<=NF;i+=2) {
            if ( $i==tgt ) {
                f = i+1
            }
        }
        max = $f
    }
    $f >= max { max=$f; name=$1 }
    END { print name }
    
    $ awk -v tgt=20150801 -f tst.awk file
    name2
    

提交回复
热议问题