简体   繁体   中英

Sorting a file with Cygwin in Windows

I have a file with thousands of beer reviews that I need to sort. Each beer review looks like this:

beer/name: John Harvards Simcoe IPA
beer/beerID: 63836
beer/brewerID: 8481
beer/ABV: 5.4
beer/style: India Pale Ale (ITA)
review/appearance: 4/5
review/aroma: 6/10
review/palate: 3/5
review/taste: 6/10
review/overall: 13/20
review/time: 11575857200

I need to sort top 10 beers with the highest number of reviews. The beer identifier 'beer/name'.

Sounds like a homework, but anyway (I like the beer), this is an starter :

awk -F'[ /]' -v note=20 '
    /^beer\/name:/{$1=$2="";beer=$0}
    /^review/ && !/review\/time/{arr[beer]+=$3/$4; count++}
    END{for (a in arr) print a, arr[a]*note/count "/" note}
' beers.txt

It's easy from this to pipe the output to the sort & head command to sort a specific column & display N lines from the top respectively. (Or use awk itself, but it's harder...)

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