RE: how to manipulate word using perl

[email protected] (Thomas Bätzler)
Newsgroups perl.beginners
Message-ID <[email protected]>
Hi,

> 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
  next unless my( $base ) = ( $name =~ m/^(.*?)\.txt$/ );
  if( -f $name ){
    rename( $name, $base . '=' ) or warn "failed to rename '$name': $!";
  } else {
    warn "file '$name' not found.";
  }
}

close( IN);

__END__

HTH,
Thomas
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.