diff --git a/bash/next.awk b/bash/next.awk index c5e19c3..e2f93a1 100644 --- a/bash/next.awk +++ b/bash/next.awk @@ -1,15 +1,18 @@ # awk script to increment the digits, preserve the zero prefix, add string descriptor if supplied, and add back extension BEGIN { -# set field separator, blank by default +# Builtin variable, "field seperator", blank by default. The neatness of this is +# the main reason I use awk to parse the filename. FS = "." } { + +# another reason why awk is great for this: newnum = $1 + 1 -nzeros = length($1) - length(int(newnum)) # could I do this with a simpler regex to select all leading zeros? +nzeros = length($1) - length(int(newnum)) leadingzeros = "" if (nzeros > 0) for (i = 1; i <= nzeros; i++) diff --git a/bash/rename.awk b/bash/rename.awk new file mode 100644 index 0000000..b90ed2f --- /dev/null +++ b/bash/rename.awk @@ -0,0 +1,27 @@ +# generates a string of "oldname newname" from 2 records, for use with mv. Main +# problem solved here generating the newname string using the number prefix and +# filetype affix. + +BEGIN { + FS="." + oldname = "" + num = "" + ext = "" + newname = "" +} + +NR == 1 { + oldname = $0 + num = $1 + # important that this is the last field, not numbered, to allow for a file + # without a string descriptor. + ext = $NF +} + +NR == 2 { + newname = sprintf("%s.%s.%s", num, $0, ext) +} + +END { + print oldname, newname +} diff --git a/bash/rename.sh b/bash/rename.sh index 2fb9cae..419f42c 100755 --- a/bash/rename.sh +++ b/bash/rename.sh @@ -12,32 +12,6 @@ # split the args by a newline so that awk reads them as separate "records" mvcmd=`echo "$1 -$2" | -awk ' -BEGIN { - # Builtin variable, "field seperator". The neatness of this is the main reason - # I use awk to parse the filename. - FS="." - oldname = "" - num = "" - ext = "" - newname = "" -} +$2" | awk -f $UMBASEPATH/rename.awk` -NR == 1 { - oldname = $0 - num = $1 - # important that this is the last field, not numbered, to allow for a file - # without a string descriptor. - ext = $NF -} - -NR == 2 { - newname = sprintf("%s.%s.%s", num, $0, ext) -} - -END { - print oldname, newname -} -'` mv $mvcmd