"Revision up" instead of version up

I need to alter this script to work with a revision instead of a version.
RF_102_209_200_v001_r01.nk is the new format and I'll be revisioning up the r01 portion and not the v001.

 #!/bin/sh  

for file in "$@" 
do         
ext=${file##*.}         
base=${file%.*}         
num=${base##*v}         
zeroes=${num%%[!0]*}         
num=${num#$zeroes}  
num=$((num+1))         
base=${base%v*}         
new=$(printf '%sv%04d.%s' "$base" "$num" "$ext")         
cp -nv "$file" "$new"
 done


  

What would be your approach, in light of the proposals you received on similar requests \

https://www.unix.com/shell-programming-and-scripting/214571-incremental-numbering.html\#post302763253

?

Yes well, unfortunately, I don't get to keep up with this stuff as often as I'd like, since this isn't really part of my job. I took a stab at it but failed miserably. So rather than have my boss come over and ask what the f' I'm doing writing code instead of actually working, I asked here.

Hey, I got lucky and worked it out myself. I thought it would be harder.

#!/bin/sh

for file in "$@"
do
        ext=${file##*.}
        base=${file%.*}
        num=${base##*[r_]}
        base=${base%$num}
        num=$((10#$num+1))
        new=$(printf '%s%02d.%s' "$base" "$num" "$ext")
        cp -nv "$file" "$new"
done

Very good indeed. If you can run the script with bash , try also

IFS="_."
for file in *.nk
  do    read A B C D V R X <<< $file
        printf "%s_%s_%s_%s_%s_r%02d.%s\n" $A $B $C $D $V $((10#${R#r} + 1)) $X
  done
RF_102_209_200_v001_r02.nk

or

IFS="_."
for file in *.nk
  do    ARR=( $file )
        printf "%s_%s_%s_%s_%s_r%02d.%s\n" ${ARR[0]} ${ARR[1]} ${ARR[2]} ${ARR[3]} ${ARR[4]} $((10#${ARR[5]#r} + 1)) ${ARR[6]}
  done
 RF_102_209_200_v001_r02.nk

Don't forget to reset IFS to its original value.

If all your file names are set to the positional parameters, try

IFS=$'\n'
while IFS="_." read A B C D V R X
  do    printf "cp %s_%s_%s_%s_%s_%s.%s %s_%s_%s_%s_%s_r%02d.%s\n" $A $B $C $D $V $R $X $A $B $C $D $V $((10#${R#r} + 1)) $X
  done <<< "$*"
cp RF_102_209_200_v001_r01.nk RF_102_209_200_v001_r02.nk
cp RF_102_209_200_v001_r04.nk RF_102_209_200_v001_r05.nk

and pipe the output into your shell...

Wow. I like the look of those.

They seem like they'd be much easier to edit being that we're just calling each piece of the file a different variable.

I don't completely understand it yet, but I'll do more research and try to figure out what exactly is happening.

Thank you.