Re: How to renumber files?

From: Dances With Crows (danSPANceswitTRAPhcrows_at_gmail.com)
Date: 01/22/05


Date: 22 Jan 2005 02:05:21 GMT

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


Relevant Pages

  • Re: function definition syntax
    ... :> In which case it might as well be a separate script - you don't need a function. ... :>: That's much more reliable than having to remember which syntax ... Always using POSIX ... :> not overlook restoring IFS. ...
    (comp.unix.shell)
  • Re: Books/References new to scripting in AD/2003
    ... my own website has lots of script samples you might find useful: ... test bits of flag attributes like userAccountControl using SQL syntax. ... To restrict the query to an OU, ...
    (microsoft.public.windows.server.scripting)
  • Re: Books/References new to scripting in AD/2003
    ... Good luck with your scripting - It's a worthwhile pursuit and the time you spend learning to script can easily be re-paid. ... I don't believe there is any way to test bits of flag attributes like userAccountControl using SQL syntax. ... but needs to be enclosed in quotes in a query. ...
    (microsoft.public.windows.server.scripting)
  • Re: Question about Sun JAVAC
    ... > The RE and ERE syntax used by various Unix utilities clearly was ... > Of course the symbols available for ASCII I/O devices (that is, ... > But traditional mathematical syntax for regular expressions happened ...
    (comp.programming)
  • Re: Legacy code/browser compatibility
    ... a user of such an ancient browsers may see a syntax error ... either script supported or not. ... browsers, script will be supported. ... that doesn't support object literals ...
    (comp.lang.javascript)

Loading