aboutsummaryrefslogtreecommitdiff
path: root/ord
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2020-11-24 02:33:30 +0100
committerJohn Ankarström <john@ankarstrom.se>2020-11-24 02:33:30 +0100
commit09c8e5f72b34b7c13f278acb52f2dd3e530558d3 (patch)
tree14b8065161759f19e649065e18b48216b2ee6bb3 /ord
downloadxutil-09c8e5f72b34b7c13f278acb52f2dd3e530558d3.tar.gz
Add 'cpy' / 'pst', 'ep' and 'ord' utilities
Diffstat (limited to 'ord')
-rwxr-xr-xord60
1 files changed, 60 insertions, 0 deletions
diff --git a/ord b/ord
new file mode 100755
index 0000000..2d0687f
--- /dev/null
+++ b/ord
@@ -0,0 +1,60 @@
+#!/bin/sh
+
+# Order files by adding/changing numbers (like "01. ") in front of their names
+
+usage() { echo usage: $0 [-n number width] [-s separator] 1>&2; exit 1; }
+
+# Default values
+n=2
+s='. '
+
+# Parse options
+while getopts n:s: o
+do
+ case $o in
+ n) n=$OPTARG ;;
+ s) s=$OPTARG ;;
+ ?) usage ;;
+ esac
+done
+shift $((OPTIND-1))
+
+# Validate options
+case "$n" in
+[1-9]) ;;
+*) echo $0: n must be a number from 1 to 9 1>&2
+ exit 1 ;;
+esac
+test -z "$1" && usage
+
+# Construct glob and regex substitution from -s and -n
+i=0; while test $((i++)) -lt "$n"; do sub=$sub'[0-9]'; done
+glob=$sub
+sub="s/^$sub$(printf '%s\n' "$s" | sed 's/\([.*[\\]\|\]\)/\\&/g')//"
+
+totext() {
+ for f in "$@"; do printf '%s\n' "$f"; done |
+ sort |
+ sed "$sub"
+}
+
+fromtext() {
+ nl -s.\ -w2 -nrz | {
+ i=1
+ while read new
+ do
+ name=`printf '%s\n' "$new" | sed "$sub"`
+ if test -e "$name"
+ then mv "$name" "$new"
+ else mv $glob"$s$name" "$new" 2>&-
+ fi
+ done
+ }
+}
+
+# Edit order
+tmp=/tmp/ord.$RANDOM
+totext "$@" > $tmp
+${EDITOR:-vi} $tmp
+fromtext < $tmp
+rm $tmp