Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: help on stripping out extra extensions

by tedrek (Pilgrim)
on Jun 21, 2003 at 00:46 UTC ( #267751=note: print w/replies, xml ) Need Help??


in reply to help on stripping out extra extensions

look at 'rename', there are two versions I'm aware of one which uses perl regexes and one which doesn't :). For the one with perl regexes something like 's/\.html$//' as the first arg and the files as the second+ arg should work. for the other version something like 'rename '.html' '' *.html' should fix it up, just be aware that that version replaces . *first* occurence.. look at the man page on you local machine to determine your version and find more options

and a pure perl solution

perl -e 'foreach (@ARGV) {next unless -f && /\.html\.html$/;($new = $_) =~ s/\.html$//;die $new if -f $new;rename $_, $new;}' *.html

I'm sure that's longer than it needs to be... :( hehe. That should do it, *note that it is untested.

Replies are listed 'Best First'.
Re^2: help on stripping out extra extensions
by Aristotle (Chancellor) on Jun 21, 2003 at 12:23 UTC
    Careful. Your first two solutions will happily remove a correct, lone .html. Rather, you should replace multiple occurences with a single one, as you (almost) did in your oneliner:
    # perl rename rename 's/(?:\.html){2,}$/.html/' *.html
    # unix rename rename .html.html .html *.html
    Your oneliner can be shortened by not checking if a substitution will be successful first; the substitution simply fails if no match is found. Untested:
    # perl oneliner perl -e '-f && ($a = $_) =~ s/(?:\.html){2,}$/.html/ && -e $new ? warn + $_ : rename $_, $new for @ARGV' *.html

    Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://267751]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2021-09-27 23:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?