Thomas Bätzler wrote:

Gowri Chandra Sekhar Barla, TLS, Chennai wrote:

I facing a problem to substitute extensions of file with " = "


For example:

In a file I have names of some files

a.txt
b.txt
c.txt

I want them to
a=
b=
c=

Do you just want to change the text in that file, or is that file
a list of filenames that you want to process?

Here's some code to do the latter:

#!/usr/bin/perl -w

use strict;

my $file = 'xxx'; # name/path to your list file

open( IN, '<', $file ) or die "Can't open '$file': $!";

while( my $name = <IN> ){
  $name =~ s/\s*$//; # remove \n and trailing whitespace

That should be:

    $name =~ s/\s+$//;

Because there is no point in modifying a string that does not contain whitespace.


  next unless my( $base ) = ( $name =~ m/^(.*?)\.txt$/ );

That should be:

    next unless my ( $base ) = $name =~ /^(.+)\.txt$/

Because '' is not a valid file name.


  if( -f $name ){

I assume that you are using the -f test because you don't want to rename directories or other non-file types?


    rename( $name, $base . '=' ) or warn "failed to rename '$name': $!";
  } else {
    warn "file '$name' not found.";

So maybe your message should indicate that:

      warn "'$name' is not a normal file.\n";


  }
}

close( IN);



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to