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:'
@ThePowerTool
Copy link

ThePowerTool commented Oct 24, 2018

I don't understand the value of this script. I realize I may just be missing the entire point so I will share my observations:
This script appears to simply perform the same function (at it's most basic level) as transmission-remote params | sort -k[x] where x= the field you wish to sort.
I tried passing a test to this script to see if it addressed what I thought was the key reason to want a script like this using:

ID     Done       Have  ETA           Up    Down  Ratio  Status       Name
 396   100%    4.16 GB  Done         0.0     0.0    0.0  Idle         Name10
 671   100%    4.74 GB  Done         0.0     0.0    0.0  Idle         Name01
 782    44%    1.54 GB  Unknown      0.0     0.0    0.0  Idle         Name05
 815    24%    1.02 GB  Unknown      0.0     0.0    0.0  Idle         Name07
 964    30%    1.09 GB  Unknown      0.0     0.0    0.0  Idle         Name04
1047     3%   118.5 MB  Unknown      0.0     0.0    0.7  Idle         Name06
1167     6%   295.9 MB  Unknown      0.0     0.0    0.0  Downloading  Name02
1203     0%       None  Unknown      0.0     0.0   None  Idle         Name03
1320    n/a       None  Unknown      0.0     0.0   None  Idle         Name09
1428     1%   50.89 MB  Unknown      0.0     0.0    0.0  Idle         Name08

The output carries forward the problem inherent in the sort command yielding:

ID     Done       Have  ETA           Up    Down  Ratio  Status       Name
 671   100%    4.74 GB  Done         0.0     0.0    0.0  Idle         Name01
1167     6%   295.9 MB  Unknown      0.0     0.0    0.0  Downloading  Name02
 964    30%    1.09 GB  Unknown      0.0     0.0    0.0  Idle         Name04
 782    44%    1.54 GB  Unknown      0.0     0.0    0.0  Idle         Name05
1047     3%   118.5 MB  Unknown      0.0     0.0    0.7  Idle         Name06
 815    24%    1.02 GB  Unknown      0.0     0.0    0.0  Idle         Name07
1428     1%   50.89 MB  Unknown      0.0     0.0    0.0  Idle         Name08
 396   100%    4.16 GB  Done         0.0     0.0    0.0  Idle         Name10
1203     0%       None  Unknown      0.0     0.0   None  Idle         Name03
1320    n/a       None  Unknown      0.0     0.0   None  Idle         Name09

@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