简体   繁体   中英

Awk: filter files based on maximum in column group

I'm trying to filter (or move) folders based on a maximum value in each group in Linux...

For Example: List of filenames in a folder and I'd like to move the one with the greatest Value by Code after sorting by SN,Code,Date

SN-Code-Date-Value  
01-2L-20200417-153542  
01-2L-20200417-155640  --> move to folder  
01-43-20200511-192316  
01-43-20200521-165949  
01-43-20200521-185815  --> move to folder  

Thanks!

sort by keys and timestamp and pick the first record by awk , which will be the first since timestamp sorting in reverse order.

$ sort -t- -k1,2 -k3,4r file | awk -F- '!a[$1,$2]++'

note that this is comparing dates as well, if you want date to be part of the key, add $3 to lookup key as well in the script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM