Skip to content

Instantly share code, notes, and snippets.

@syphoxy
Last active February 29, 2024 23:30
Show Gist options
  • Save syphoxy/8338067 to your computer and use it in GitHub Desktop.
Save syphoxy/8338067 to your computer and use it in GitHub Desktop.
sort transmission-remote -l output
#!/bin/bash
# Default parameters
OptSortColumn=none
OptSortReverse=0
ProxyArguments=()
# Parse parameters
for param in "$@"
do
if echo "$param" | egrep -q "^--sort="
then
OptSortColumn="$(echo "$param" | cut -d= -f2)"
if echo "$OptSortColumn" | egrep -q '^-(id|done|have|eta|up|down|ratio|status|name)$'
then
OptSortColumn="$(echo "$OptSortColumn" | tr -d "-")"
OptSortReverse=1
fi
else
ProxyArguments+=("$param")
fi
done
# if sorting isn't given, just run transmission
if [ "$OptSortColumn" == "none" ]
then
transmission-remote "$@"
exit
fi
# Store transmission torrent table for manipulation
TorrentList="$(transmission-remote "$ProxyArguments")"
# Get sort key index
SortKey=$(echo "id done have eta up down ratio status name" \
| tr ' ' '\n' \
| cat -n \
| awk -v "key=$OptSortColumn" 'key == $2 { print $1 }')
# Account for Have column data having "two columns"
if [ "$SortKey" -gt 3 ]
then
SortKey=$[ $SortKey + 1 ]
fi
# Render table header
echo "$TorrentList" \
| grep --color=never '^ID'
# Render sorted active torrents
for id in $(echo "$TorrentList" \
| awk '($3 != "None" && ($1 != "ID" && $1 != "Sum:")) { gsub("*", "", $1); print $'$SortKey'" "$1 }' \
| sort -n $([ "$OptSortReverse" == 1 ] && echo -n "-r")\
| awk '{print $2}')
do
echo "$TorrentList" \
| awk '$1 ~ /^'$id'\*?$/ {print $0}'
done
# Render inactive torrents
echo "$TorrentList" \
| awk '$3 == "None" {print $0}'
# Render table footer
echo "$TorrentList" \
| grep --color=never '^Sum:'
@mbakumov
Copy link

# set permissions
chmod +x transmission-sorted.sh

# examples
transmission-sorted.sh -l --sort=ratio
transmission-sorted.sh -l --sort=name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment