Skip to content

Instantly share code, notes, and snippets.

@dcarneiro
Created April 24, 2015 09:42
Show Gist options
  • Save dcarneiro/28cc4b06787ef1fe8ae4 to your computer and use it in GitHub Desktop.
Save dcarneiro/28cc4b06787ef1fe8ae4 to your computer and use it in GitHub Desktop.
# This shell will get you the top x items for each group
# pe:
# if you have a csv with user_id, song_id, relevance
# this will sort the csv by user_id asc and then by relevance desc
# after that it will only print the top x results for each user to STDOUT
sort -t, -k 1,1n -k 3,3nr my.csv | awk -F, -v max_items=5 '{if (user_id != $1) {user_id=$1; count=1} ; if (count <= max_items) { print; } ; count+=1;}' > result.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment