Re: How to renumber files?
From: Dances With Crows (danSPANceswitTRAPhcrows_at_gmail.com)
Date: 01/22/05
- Next message: Steve Wolfe: "Re: RAID-5 Double Disk Failure"
- Previous message: Chris Carlen: "Re: How to renumber files?--Uh Oh!"
- In reply to: Chris Carlen: "Re: How to renumber files?"
- Next in thread: Chris Carlen: "Re: How to renumber files?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Steve Wolfe: "Re: RAID-5 Double Disk Failure"
- Previous message: Chris Carlen: "Re: How to renumber files?--Uh Oh!"
- In reply to: Chris Carlen: "Re: How to renumber files?"
- Next in thread: Chris Carlen: "Re: How to renumber files?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|