rename.awk
This commit is contained in:
parent
91b8272555
commit
a725b5c0de
3 changed files with 33 additions and 29 deletions
|
|
@ -1,15 +1,18 @@
|
||||||
# awk script to increment the digits, preserve the zero prefix, add string descriptor if supplied, and add back extension
|
# awk script to increment the digits, preserve the zero prefix, add string descriptor if supplied, and add back extension
|
||||||
|
|
||||||
BEGIN {
|
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 = "."
|
FS = "."
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
||||||
|
# another reason why awk is great for this:
|
||||||
newnum = $1 + 1
|
newnum = $1 + 1
|
||||||
nzeros = length($1) - length(int(newnum))
|
|
||||||
|
|
||||||
# could I do this with a simpler regex to select all leading zeros?
|
# could I do this with a simpler regex to select all leading zeros?
|
||||||
|
nzeros = length($1) - length(int(newnum))
|
||||||
leadingzeros = ""
|
leadingzeros = ""
|
||||||
if (nzeros > 0)
|
if (nzeros > 0)
|
||||||
for (i = 1; i <= nzeros; i++)
|
for (i = 1; i <= nzeros; i++)
|
||||||
|
|
|
||||||
27
bash/rename.awk
Normal file
27
bash/rename.awk
Normal 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
|
||||||
|
}
|
||||||
|
|
@ -12,32 +12,6 @@
|
||||||
|
|
||||||
# split the args by a newline so that awk reads them as separate "records"
|
# split the args by a newline so that awk reads them as separate "records"
|
||||||
mvcmd=`echo "$1
|
mvcmd=`echo "$1
|
||||||
$2" |
|
$2" | awk -f $UMBASEPATH/rename.awk`
|
||||||
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 = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
mv $mvcmd
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue