Finding max value of a specific date awk

后端 未结 3 696
天涯浪人
天涯浪人 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:37

    As a quick&dirty solution, we can perform this in following Unix commands:

    yourdatafile=
    yourdate=
    
    cat $yourdatafile | sed 's/|/_/g' | awk -F "${yourdate}_" '{print $1" "$2}' | sed 's/[0-9]*_[0-9]*//g' | awk '{print $1" "$2}' |sort -k 2n | tail -n 1
    

    With following sample data:

    $ cat $yourdatafile
    Alice 20150801|44 20150802|21  20150803|7  20150804|76  20150805|71
    Bob 20150801|31 20150802|5 20150803|21 20150804|133 20150805|71
    

    and yourdate=20150803 we get:

    $ cat $yourdatafile | sed 's/|/_/g' | awk -F "${yourdate}_" '{print $1" "$2}' | sed 's/[0-9]*_[0-9]*//g' | awk '{print $1" "$2}' |sort -k 2n | tail -n 1
    Bob 21
    

    and for yourdate=20150802 we get:

    $ cat $yourdatafile | sed 's/|/_/g' | awk -F "${yourdate}_" '{print $2" "$1}' | sed 's/[0-9]*_[0-9]*//g' | awk '{print $2" "$1}' | sort -k 2n | tail -n 1
    Alice 21
    

    The drawback is that only one line is printed the highest value of a day was achieved by more than one name as can be seen with:

    $ yourdate=20150805; cat $yourdatafile | sed 's/|/_/g' | awk -F "${yourdate}_" '{print $2" "$1}' | sed 's/[0-9]*_[0-9]*//g' | awk '{print $2" "$1}' | sort -k 2n | tail -n 1
    Bob 71
    

    I hope that helps anyway.

提交回复
热议问题