How to renumber files?

  • Follow


Hi:

Photos from my digital camera are named like this:

crw_0001.crw
crw_0002.crw
crw_0003.crw
crw_0004.crw
etc.

Let's say I have two CF cards, both with a name sequence that begins at 
_0001.  I want to merge the contents of the two cards into one directory 
without the second card's files overwriting the first.

How can I rename the files from the second card so that the 4-digit 
numeric field is renumbered starting at a value of my choosing (which 
would likely be 1+ the last number from the first card)?

For instance, I want to rename the above files to:

crw_0005.crw
crw_0006.crw
crw_0007.crw
crw_0008.crw
etc.


Any ideas?


Thanks.




-- 
_____________________
Christopher R. Carlen
crobc@sbcglobal.net
SuSE 9.1 Linux 2.6.5
0
Reply crobc6424 (46) 1/21/2005 7:21:58 PM

On Fri, 21 Jan 2005 11:21:58 -0800, Chris Carlen staggered into the
Black Sun and said:
> Photos from my digital camera are named like this:
> crw_0001.crw crw_0002.crw crw_0003.crw crw_0004.crw
>
> Let's say I have two CF cards, both with a name sequence that begins
> at _0001.  I want to merge the contents of the two cards into one
> directory without the second card's files overwriting the first.  How
> can I rename the files from the second card so that the 4-digit
> numeric field is renumbered starting at a value of my choosing?
>
> For instance, I want to rename the above files to:
> crw_0005.crw  crw_0006.crw  crw_0007.crw  crw_0008.crw
> Any ideas?

Try something like this:

#!/bin/bash
# place in ~/bin/renamecrw.sh ; chmod +x it
# call with "renamecrw.sh 5" to add 5 to the index number of every .crw
# file in the current directory
for FILE in *.crw ; do
	NUM=`echo $FILE | sed -e 's/crw_//' -e 's/.crw//' `
	let NUM=$NUM+$1
	NEWFILE=`printf "crw_%04d.crw" $NUM `
	mv $FILE $NEWFILE
	done
# end of script

....there are probably better ways to do this; this is just what I
thought of in ~30 seconds.  I think "rename" won't work here since it's
not complex enough, though there's probably a more intelligent
file-renaming thingy out there--I've just forgotten its name.  HTH,

-- 
Matt G|There is no Darkness in Eternity/But only Light too dim for us to see
Brainbench MVP for Linux Admin /    mail: TRAP + SPAN don't belong
http://www.brainbench.com     /                Hire me! 
-----------------------------/ http://crow202.dyndns.org/~mhgraham/resume
0
Reply danSPANceswitTRAPhcrows (1928) 1/21/2005 8:22:00 PM


On Fri, 21 Jan 2005 at 19:21 GMT, Chris Carlen wrote:
> Hi:
> 
> Photos from my digital camera are named like this:
> 
> crw_0001.crw
> crw_0002.crw
> crw_0003.crw
> crw_0004.crw
> etc.
> 
> Let's say I have two CF cards, both with a name sequence that begins at 
> _0001.  I want to merge the contents of the two cards into one directory 
> without the second card's files overwriting the first.
> 
> How can I rename the files from the second card so that the 4-digit 
> numeric field is renumbered starting at a value of my choosing (which 
> would likely be 1+ the last number from the first card)?
> 
> For instance, I want to rename the above files to:
> 
> crw_0005.crw
> crw_0006.crw
> crw_0007.crw
> crw_0008.crw
> etc.

DEST=/path/to/new/directory
X=4  ## Amount to increment each number
for file in *.crw
do
   num=${file%.crw}  ## remove .crw from end
   num=${num#crw_}   ## remove crw_ from beginning
   case $num in  ## Remove leading zeros and add increment
      0000) newnum=$X ;;
      000?) newnum=$(( ${num#000} + $X )) ;;
      00??) newnum=$(( ${num#00} + $X )) ;;
      0???) newnum=$(( ${num#0} + $X )) ;;
      *) newnum=$(( $num + $X )) ;;
   esac
   mv "$file" "$DEST/crw_$newnum.crw"
done

-- 
    Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
    ===================================================================
    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License
0
Reply cfajohnson (1783) 1/21/2005 8:35:51 PM

On Fri, 21 Jan 2005 20:22:00 +0000, Dances With Crows wrote:

> On Fri, 21 Jan 2005 11:21:58 -0800, Chris Carlen staggered into the
> Black Sun and said:
>> Photos from my digital camera are named like this:
>> crw_0001.crw crw_0002.crw crw_0003.crw crw_0004.crw
>>
>> Let's say I have two CF cards, both with a name sequence that begins
>> at _0001.  I want to merge the contents of the two cards into one
>> directory without the second card's files overwriting the first.  How
>> can I rename the files from the second card so that the 4-digit
>> numeric field is renumbered starting at a value of my choosing?
>>
>> For instance, I want to rename the above files to:
>> crw_0005.crw  crw_0006.crw  crw_0007.crw  crw_0008.crw
>> Any ideas?
> 
> Try something like this:
> 
> #!/bin/bash
> # place in ~/bin/renamecrw.sh ; chmod +x it
> # call with "renamecrw.sh 5" to add 5 to the index number of every .crw
> # file in the current directory
> for FILE in *.crw ; do
> 	NUM=`echo $FILE | sed -e 's/crw_//' -e 's/.crw//' `
> 	let NUM=$NUM+$1
> 	NEWFILE=`printf "crw_%04d.crw" $NUM `
> 	mv $FILE $NEWFILE
> 	done
> # end of script
> 
> ...there are probably better ways to do this; this is just what I
> thought of in ~30 seconds.  I think "rename" won't work here since it's
> not complex enough, though there's probably a more intelligent
> file-renaming thingy out there--I've just forgotten its name.  HTH,

I recall doing this kind of thing using "expr" (yeh, old timer, I know
you use the shell more effectively than I do) to calculate the new
sequence number. However, in situations like this, I personally would use
rename, starting the next sequence at some convenient boundary, e.g. 0100?
1000?. In the past, I have found it useful to retain a hint of where the
boundary was, by retaining a gap in the numbering. In retrospect, you may
discover there are differences between the two sets, and you may want to
retroactively apply some selective processing... but, as they say, YMMV.

-- 
Juhan Leemet
Logicognosis, Inc.

0
Reply juhan (466) 1/21/2005 10:03:11 PM

Dances With Crows wrote:
> Try something like this:
> 
> #!/bin/bash
> # place in ~/bin/renamecrw.sh ; chmod +x it
> # call with "renamecrw.sh 5" to add 5 to the index number of every .crw
> # file in the current directory

Ok, I like the idea of being able to pass the increment amount as a 
parameter.

Lemme see if I understand this right:

> for FILE in *.crw ; do

The *.crw is expanded by bash to the list of files, and each is in turn 
assigned to var FILE upon which...

> 	NUM=`echo $FILE | sed -e 's/crw_//' -e 's/.crw//' `

The var NUM is assigned a value equal to just the numeric digits portion 
of the file name by the magic sed command whose syntax I don't 
understand.  I surmise it is doing something like: strip away the "crw_" 
part and strip away the ".crw" part, leaving just the number digits.

> 	let NUM=$NUM+$1

Add the parameter from the command line.  Oh god is the shell wierd. 
Being a moderately experienced C programmer for mostly embedded stuff, 
this being able to add two variables that a moment ago were used as if 
their types were strings, is very hard to digest.

> 	NEWFILE=`printf "crw_%04d.crw" $NUM `

Make a new file name by printf-ing a string containing the before and 
after text fields again, and sticking the value of NUM in the middle as 
a 4-digit decimal value.  Could have some fun with this, and make my 
wife crazy, by choosing a hex or binary format spec. ;-)

> 	mv $FILE $NEWFILE
> 	done
> # end of script

Ok, I get the point.

Hmm, I can almost figure out how to modify this to do the other steps in 
my process of straightening out the filenames.  I also usually do a:

rename crw_ 012005- *

to strip the unneeded pre-text, and replace it with the date.

I suppose I can try to make the script do this automagically for me.

If I can't figure it out, I'll be back.  But I won't complain if the 
clues are provided anyway.

Thanks for the input!


Good day!


-- 
_____________________
Christopher R. Carlen
crobc@sbcglobal.net
SuSE 9.1 Linux 2.6.5
0
Reply crobc6424 (46) 1/22/2005 12:26:40 AM

Dances With Crows wrote:
> 	NUM=`echo $FILE | sed -e 's/crw_//' -e 's/.crw//' `


Is there a way to not hardcode dependence on a specific initial text 
field, and .extension field, but instead just pick out the numeric part 
of any filename of the basic structure:

beforeNNNNafter

Where NNNN is the numeric digits part which may be any number of digits.

"before" is alphabetic or punctuation before the numbers, that may also 
not even exist.

"after" is also alphabetic or punctuation after the numbers which may 
also not exist.

So it could work correctly on any of these:

1
003.pic
file0001.ext
data456.bin
stuff-99

Also what would happen if we incremented the "99" in the last example to 
a three digit number?

If we can do this, it's a fairly presentable "renum" command!

Thanks for input.


Good day.




-- 
_____________________
Christopher R. Carlen
crobc@sbcglobal.net
SuSE 9.1 Linux 2.6.5
0
Reply crobc6424 (46) 1/22/2005 12:41:20 AM

In comp.os.linux.misc Chris Carlen <crobc@bogusfield.sbcglobal.net>:
> Hi:

> Photos from my digital camera are named like this:

> crw_0001.crw
> crw_0002.crw
> crw_0003.crw
[..]

> How can I rename the files from the second card so that the 4-digit 
> numeric field is renumbered starting at a value of my choosing (which 
> would likely be 1+ the last number from the first card)?

> For instance, I want to rename the above files to:

> crw_0005.crw
> crw_0006.crw
[..]

 ls crw_[0-9]* | awk 'BEGIN{FS="[0-9]+"}{printf "mv "$0" ";
 printf "%s%4.4d%s",$1,NR+4,$NF"\n"}' > file

The above should be a single line, got wrapped. Check file if
things are fine then run it:

 sh file

Might give some problems if numbers are missing.;)

Good luck

-- 
Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94)
mail: echo zvpunry@urvzvat.qr | perl -pe 'y/a-z/n-za-m/'
#bofh excuse 441: Hash table has woodworm
0
Reply USENET22 (5462) 1/22/2005 1:04:35 AM

Dances With Crows wrote:
> Try something like this:
> 
> #!/bin/bash
> # place in ~/bin/renamecrw.sh ; chmod +x it
> # call with "renamecrw.sh 5" to add 5 to the index number of every .crw
> # file in the current directory
> for FILE in *.crw ; do
> 	NUM=`echo $FILE | sed -e 's/crw_//' -e 's/.crw//' `
> 	let NUM=$NUM+$1
> 	NEWFILE=`printf "crw_%04d.crw" $NUM `
> 	mv $FILE $NEWFILE
> 	done
> # end of script

Problems:

user1@ting:/photos/300d-photos/raw-jpeg/test> l
crw_0012.crw  renum
user1@ting:/photos/300d-photos/raw-jpeg/test> ./renum 10
user1@ting:/photos/300d-photos/raw-jpeg/test> l
crw_0020.crw  renum
user1@ting:/photos/300d-photos/raw-jpeg/test> ./renum 100
user1@ting:/photos/300d-photos/raw-jpeg/test> l
crw_0116.crw  renum
user1@ting:/photos/300d-photos/raw-jpeg/test> ./renum 100
user1@ting:/photos/300d-photos/raw-jpeg/test> l
crw_0178.crw  renum
user1@ting:/photos/300d-photos/raw-jpeg/test> ./renum 10
../renum: line 8: let: NUM=0178: value too great for base (error token is 
"0178")
../renum: line 1: printf: 0178: invalid number
user1@ting:/photos/300d-photos/raw-jpeg/test> l
crw_0000.crw  renum


It would seem that the math on text strings is a little dicey.

Any ideas how to fix?


Thanks.

-- 
_____________________
Christopher R. Carlen
crobc@sbcglobal.net
SuSE 9.1 Linux 2.6.5
0
Reply crobc6424 (46) 1/22/2005 1:21:48 AM

On Fri, 21 Jan 2005 16:26:40 -0800, Chris Carlen staggered into the
Black Sun and said:
> Dances With Crows wrote:
>> #!/bin/bash
>> # place in ~/bin/renamecrw.sh ; chmod +x it
>> # call with "renamecrw.sh 5" to add 5 to the index number of every .crw
>> # file in the current directory
>
> Lemme see if I understand this right:
>> for FILE in *.crw ; do
>> 	NUM=`echo $FILE | sed -e 's/crw_//' -e 's/.crw//' `
> The var NUM is assigned a value equal to just the numeric digits
> portion of the file name by the magic sed command whose syntax I don't
> understand.

sed -e $REGEX :  apply regular expression REGEX to stardard input.  You
can use multiple -e arguments to apply multiple regexes to the input.
There's no man page for regular expressions, but "man perlre" provides a
nice reference for Perl-compatible regular expressions.  sed doesn't
really use PCRE AFAIK, but the basics hold.

> I surmise it is doing something like: strip away the "crw_" part and
> strip away the ".crw" part, leaving just the number digits.

Yep, that's it.

>> 	let NUM=$NUM+$1
> Add the parameter from the command line.  Oh god is the shell wierd.
> Being a moderately experienced C programmer for mostly embedded stuff,
> this being able to add two variables that a moment ago were used as if
> their types were strings, is very hard to digest.

Type-safety is mostly a C and Java preoccupation.  Calling atoi() would
be...  safer, but more work.  I find bash's syntax kind of weird, but
manageable for small things.  For anything long, or anything
complicated, Perl works better.  (If you think bash's syntax is "weird"
or "sloppy", you'll really get a kick out of Perl's TMTOWTDI approach.)

>> 	NEWFILE=`printf "crw_%04d.crw" $NUM `
>> 	mv $FILE $NEWFILE
>> 	done
>> # end of script
> Hmm, I can almost figure out how to modify this to do the other steps
> in my process of straightening out the filenames.  I also usually do
>
> rename crw_ 012005- *
>
> to strip the unneeded pre-text, and replace it with the date.  I
> suppose I can try to make the script do this automagically for me.  If
> I can't figure it out, I'll be back.  But I won't complain if the
> clues are provided anyway.

OK, one thing that I didn't test in the ~2 minutes I spent writing the
original message was longer numbers.  bash interprets integers that
start with 0 as octal (for obvious reasons) so that's behind the "weird
math" you were seeing earlier.  You can run NUM through a sed command
that removes leading zeroes, and that'd make things work much better.
Like so:

NUM=`echo $NUM | sed -e 's/^0*//' `

....put that before the "let" and things should work much better.  HTH,

-- 
Matt G|There is no Darkness in Eternity/But only Light too dim for us to see
Brainbench MVP for Linux Admin /    mail: TRAP + SPAN don't belong
http://www.brainbench.com     /                Hire me! 
-----------------------------/ http://crow202.dyndns.org/~mhgraham/resume
0
Reply danSPANceswitTRAPhcrows (1928) 1/22/2005 2:05:21 AM

On Sat, 22 Jan 2005 at 01:21 GMT, Chris Carlen wrote:
> user1@ting:/photos/300d-photos/raw-jpeg/test> ./renum 10
> ./renum: line 8: let: NUM=0178: value too great for base (error token is 
> "0178")
> ./renum: line 1: printf: 0178: invalid number
> user1@ting:/photos/300d-photos/raw-jpeg/test> l
> crw_0000.crw  renum
> 
> 
> It would seem that the math on text strings is a little dicey.
> 
> Any ideas how to fix?

   The leading zeros must be removed, or they will be interpreted as
   octal. and 08 and 09 are not valid octal numbers.

   Here's my solution, again, which deals with the issue (note that
   there is no need for any external utility to break apart the string
   and do the arithmeitc. Also note that I do not use command
   substitution (`...`) as it is very slow in bash:

DEST=/path/to/new/directory
X=4  ## Amount to increment each number
for file in *.crw
do
   num=${file%.crw}  ## remove .crw from end
   num=${num#crw_}   ## remove crw_ from beginning
   case $num in  ## Remove leading zeros and add increment
      0000) newnum=$X ;;
      000?) newnum=$(( ${num#000} + $X )) ;;
      00??) newnum=$(( ${num#00} + $X )) ;;
      0???) newnum=$(( ${num#0} + $X )) ;;
      *) newnum=$(( $num + $X )) ;;
   esac
   mv "$file" "$DEST/crw_$newnum.crw"
done

   If you want to generalize it, put crw_ and .crw in variables,
   instead of hard coding them.

-- 
    Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
    ===================================================================
    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License
0
Reply cfajohnson (1783) 1/22/2005 2:46:22 AM

On Sat, 22 Jan 2005 at 02:46 GMT, Chris F.A. Johnson wrote:
> On Sat, 22 Jan 2005 at 01:21 GMT, Chris Carlen wrote:
>> user1@ting:/photos/300d-photos/raw-jpeg/test> ./renum 10
>> ./renum: line 8: let: NUM=0178: value too great for base (error token is 
>> "0178")
>> ./renum: line 1: printf: 0178: invalid number
>> user1@ting:/photos/300d-photos/raw-jpeg/test> l
>> crw_0000.crw  renum
>> 
>> 
>> It would seem that the math on text strings is a little dicey.
>> 
>> Any ideas how to fix?
> 
>    The leading zeros must be removed, or they will be interpreted as
>    octal. and 08 and 09 are not valid octal numbers.
> 
>    Here's my solution, again, which deals with the issue (note that
>    there is no need for any external utility to break apart the string
>    and do the arithmeitc. Also note that I do not use command
>    substitution (`...`) as it is very slow in bash:
> 
> DEST=/path/to/new/directory
> X=4  ## Amount to increment each number
> for file in *.crw
> do
>    num=${file%.crw}  ## remove .crw from end
>    num=${num#crw_}   ## remove crw_ from beginning
>    case $num in  ## Remove leading zeros and add increment
>       0000) newnum=$X ;;
>       000?) newnum=$(( ${num#000} + $X )) ;;
>       00??) newnum=$(( ${num#00} + $X )) ;;
>       0???) newnum=$(( ${num#0} + $X )) ;;
>       *) newnum=$(( $num + $X )) ;;
>    esac
>    mv "$file" "$DEST/crw_$newnum.crw"
> done
> 
>    If you want to generalize it, put crw_ and .crw in variables,
>    instead of hard coding them.

    It doesn't deal with padding, however. That can be done with
    another case statement:

case $newnum in
     ?) newnum=000$newnum ;;
     ??) newnum=00$newnum ;;
     ???) newnum=0$newnum ;;
esac

    Or with parameter expansion:

newnum=0000$newnum
newnum=${newnum#${newnum%????}}


-- 
    Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
    ===================================================================
    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License
0
Reply cfajohnson (1783) 1/22/2005 3:00:14 AM

� Chris Carlen <crobc@BOGUSFIELD.sbcglobal.net>:

> Hi:
> 
> Photos from my digital camera are named like this:
> 
> crw_0001.crw
> crw_0002.crw
> crw_0003.crw
> crw_0004.crw
> etc.
> 
> Let's say I have two CF cards, both with a name sequence that begins at 
> _0001.  I want to merge the contents of the two cards into one directory 
> without the second card's files overwriting the first.
> 
> How can I rename the files from the second card so that the 4-digit 
> numeric field is renumbered starting at a value of my choosing (which 
> would likely be 1+ the last number from the first card)?
> 
> For instance, I want to rename the above files to:
> 
> crw_0005.crw
> crw_0006.crw
> crw_0007.crw
> crw_0008.crw
> etc.
> 
> 
> Any ideas?

Yep.

#! /bin/bash
current=1
start=4
for image in crw_*.crw; do
 number=$(printf %04d $(($start + $current)) )
 mv "$image" "crw_${number}.crw"
 current=$(($current + 1))
done

Alexander Skwar
-- 
Be careful what you set your heart on -- for it will surely be yours.
  -- James Baldwin, "Nobody Knows My Name"
________________________________________________________________________

0
Reply alexander930 (342) 1/22/2005 8:28:08 AM

In article <35dt3uF4ki4o5U1@individual.net>,
Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
:
:   The leading zeros must be removed, or they will be interpreted as
:   octal. and 08 and 09 are not valid octal numbers.
:
:   Here's my solution, again, which deals with the issue (note that
:   there is no need for any external utility to break apart the string
:   and do the arithmeitc. Also note that I do not use command
:   substitution (`...`) as it is very slow in bash:
:
:DEST=/path/to/new/directory
:X=4  ## Amount to increment each number
:for file in *.crw
:do
:   num=${file%.crw}  ## remove .crw from end
:   num=${num#crw_}   ## remove crw_ from beginning
:   case $num in  ## Remove leading zeros and add increment
:      0000) newnum=$X ;;
:      000?) newnum=$(( ${num#000} + $X )) ;;
:      00??) newnum=$(( ${num#00} + $X )) ;;
:      0???) newnum=$(( ${num#0} + $X )) ;;
:      *) newnum=$(( $num + $X )) ;;
:   esac
:   mv "$file" "$DEST/crw_$newnum.crw"
:done

Why not simply tell bash that it's a decimal number:

       newnum=$(( 10#$num + X ))

But, since the OP indicated a wish to preserve the leading zeros:

    DEST=/path/to/new/directory
    X=4  ## Amount to increment each number
    ZPAD=0000000000
    for file in *.crw
    do
       num=${file%.crw}  ## remove .crw from end
       num=${num#crw_}   ## remove crw_ from beginning
       newnum=$(( 10#$num  + $X ))
       pad=$(( ${#num} - ${#newnum} ))    ## the length difference
       [ $pad -gt 0 ] && newnum=${ZPAD:0:$pad}$newnum
       mv "$file" "$DEST/crw_$newnum.crw"
    done


-- 
Bob Nichols         AT comcast.net I am "rnichols42"
0
Reply SEE_SIGNATURE1 (213) 1/22/2005 4:06:09 PM

On Sat, 22 Jan 2005 at 16:06 GMT, Robert Nichols wrote:
> In article <35dt3uF4ki4o5U1@individual.net>,
> Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
>:
>:   The leading zeros must be removed, or they will be interpreted as
>:   octal. and 08 and 09 are not valid octal numbers.
>:
>:   Here's my solution, again, which deals with the issue (note that
>:   there is no need for any external utility to break apart the string
>:   and do the arithmeitc. Also note that I do not use command
>:   substitution (`...`) as it is very slow in bash:
>:
>:DEST=/path/to/new/directory
>:X=4  ## Amount to increment each number
>:for file in *.crw
>:do
>:   num=${file%.crw}  ## remove .crw from end
>:   num=${num#crw_}   ## remove crw_ from beginning
>:   case $num in  ## Remove leading zeros and add increment
>:      0000) newnum=$X ;;
>:      000?) newnum=$(( ${num#000} + $X )) ;;
>:      00??) newnum=$(( ${num#00} + $X )) ;;
>:      0???) newnum=$(( ${num#0} + $X )) ;;
>:      *) newnum=$(( $num + $X )) ;;
>:   esac
>:   mv "$file" "$DEST/crw_$newnum.crw"
>:done
> 
> Why not simply tell bash that it's a decimal number:
> 
>        newnum=$(( 10#$num + X ))
> 
> But, since the OP indicated a wish to preserve the leading zeros:
> 
>     DEST=/path/to/new/directory
>     X=4  ## Amount to increment each number
>     ZPAD=0000000000
>     for file in *.crw
>     do
>        num=${file%.crw}  ## remove .crw from end
>        num=${num#crw_}   ## remove crw_ from beginning
>        newnum=$(( 10#$num  + $X ))
>        pad=$(( ${#num} - ${#newnum} ))    ## the length difference
>        [ $pad -gt 0 ] && newnum=${ZPAD:0:$pad}$newnum
>        mv "$file" "$DEST/crw_$newnum.crw"
>     done

   This is fine in bash, but I prefer to use a more portable syntax
   whenever possible. The two lines that are not POSIX compatible are:

newnum=$(( 10#$num  + $X ))
[ $pad -gt 0 ] && newnum=${ZPAD:0:$pad}$newnum


-- 
    Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
    ===================================================================
    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License
0
Reply cfajohnson (1783) 1/22/2005 8:32:03 PM

In article <35fri3F4jhg4qU1@individual.net>,
Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
:On Sat, 22 Jan 2005 at 16:06 GMT, Robert Nichols wrote:
:> 
:> Why not simply tell bash that it's a decimal number:
:> 
:>        newnum=$(( 10#$num + X ))
:> 
:> But, since the OP indicated a wish to preserve the leading zeros:
:> 
:>     DEST=/path/to/new/directory
:>     X=4  ## Amount to increment each number
:>     ZPAD=0000000000
:>     for file in *.crw
:>     do
:>        num=${file%.crw}  ## remove .crw from end
:>        num=${num#crw_}   ## remove crw_ from beginning
:>        newnum=$(( 10#$num  + $X ))
:>        pad=$(( ${#num} - ${#newnum} ))    ## the length difference
:>        [ $pad -gt 0 ] && newnum=${ZPAD:0:$pad}$newnum
:>        mv "$file" "$DEST/crw_$newnum.crw"
:>     done
:
:   This is fine in bash, but I prefer to use a more portable syntax
:   whenever possible. The two lines that are not POSIX compatible are:
:
:newnum=$(( 10#$num  + $X ))
:[ $pad -gt 0 ] && newnum=${ZPAD:0:$pad}$newnum

A reasonable goal.  I pondered whether I should restrict myself to less
bash-specific syntax, but then noted that this is a Linux newsgroup and
the proposed script that started this subthread began with
"#!/bin/bash".  Actually, I believe that syntax also requires
BASH_VERSION >= 2.00.

-- 
Bob Nichols         AT comcast.net I am "rnichols42"
0
Reply SEE_SIGNATURE1 (213) 1/23/2005 1:07:30 AM

14 Replies
37 Views

(page loaded in 0.593 seconds)


Reply: