[1+1=2]

OneAndOneIs2

« BBC goes DRM?Screenshots »

Wed, Sep 16, 2009

[Icon][Icon]Bulk file renames

• Post categories: Omni, FOSS, Rant, Technology, My Life, Helpful

I need a "Notes to self" category for these posts... But then I'd have to go back through over a thousand posts to add the tag..

I seem to need to rename batches of files quite a lot these days. The command-line isn't always the best way to do this when it's complicated stuff, so I found a helpful bit of software to use - it lets you see what the currently-defined operation would do to the files before it does it.

For simple stuff, like cropping a few characters out of the same file, no explanation is needed. But regexp things can be trickier, as ever. The tricky bit was in working out how to call text from wild-carded searches.

So if you have, say:

XXXfile1.jpg
fileXXX2.jpg
file3XXX.jpg

and you want to remove the 'XXX' from all three, the search .*XXX.* will work, but is useless because it only has one element. You need it to be THREE elements: The bit before XXX, the bit after XXX, and the XXX itself.

So you need to use (.*)(XXX)(.*) as the search term

And then use \1\3 as the replace term, to tell it to use everything before and after the XXX, but not the XXX itself.

Very simple and fairly standard in principle, but every damn language seems to have its own way of referring to elements - no brackets needed, different brackets needed, no period before the wildcard, use %1 or \1 or $1 to refer to the first element... the principle may always be the same but the phrasing of it varies enormously.

Hence the need to write this little reminder...

2 comments

Timmy
Comment from: Timmy [Visitor] Email · http://blog.tsmacdonald.com
I like how OpenOffice lets you search with regexes--I wish this was a standard feature throughout all software, so that "power-users" and "geeks" would become competent working with them.

Also, I wish that using characters worked the same way always.
16/09/09 @ 02:14
oneandoneis2
Comment from: oneandoneis2 [Member] · http://geekblog.oneandoneis2.org/
Actually, just about everything I use *does* let me use regexes.. :o)

One of the things I like most about Vi is how consistent it is across the CLI. Search for a word in vi?

/word

Search for that word in, say, less:

/word

Search and replace in vi?

:%s/old/new/g

Search and replace in sed?

s/old/new/g

And so on and so forth: Learn Vi and you learn the CLI. It's very helpful to learn that standard, and other text editors just don't let you do that..
16/09/09 @ 04:50

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)
 

[Links][icon] My links

[Icon][Icon] Hmm.. new look for twitter? I hope it gets less "Ick! Change! Put it back!" nonsense than Facebook..
08/02/12

[Icon][Icon] Facebook Syndication Error
11/02/12

[Icon][Icon] I last listened to:
Johann Pachelbel - Canon in D major

[Icon][Icon] Most recent photo:
Submersible houseboat

[Icon][Icon]About Me

[Icon][Icon]About this blog

[Icon][Icon]My LQ profile

[Icon][Icon]My /. profile

[Icon][Icon]My Wishlist

[Icon]MyCommerce

[FSF Associate Member]


February 2012
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29        

Search

User tools

XML Feeds

eXTReMe Tracker

Valid XHTML 1.0 Transitional

Valid CSS!

[Valid RSS feed]

powered by b2evolution