Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hello everyone I have a quick question for all of you I have a file like so being generated through a content management application file1.html.html ... how can i strip out the extra .html at the end thanks to everyone for the help

Replies are listed 'Best First'.
Re: help on stripping out extra extensions
by daeve (Deacon) on Jun 21, 2003 at 03:43 UTC
    I suspect that you are asking the wrong question. You should be asking where is the extra .html coming from and how can I eliminate it at the source. Take a look at your config file for a file_name.html where it's only asking for the file_name. I've been down a simular road a few times and in every case it was something that I had overlooked or added to what was requested in the config file.

    HTH
    Daeve

Re: help on stripping out extra extensions
by tedrek (Pilgrim) on Jun 21, 2003 at 00:46 UTC

    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.

      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.