Last active
February 29, 2024 23:30
-
-
Save syphoxy/8338067 to your computer and use it in GitHub Desktop.
sort transmission-remote -l output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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:' |
# 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
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:
The output carries forward the problem inherent in the sort command yielding: