Finding max value of a specific date awk

半世苍凉 提交于 2019-12-02 14:51:11

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.

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

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

yourdatafile=<yourdatafile>
yourdate=<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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!