rename.awk

This commit is contained in:
bth 2024-01-14 11:53:41 -08:00
commit a725b5c0de
3 changed files with 33 additions and 29 deletions

View file

@ -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++)

27
bash/rename.awk Normal file
View file

@ -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
}

View file

@ -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