Mr. Shawn H. Corey wrote:
On Wed, 2008-09-03 at 14:55 +0100, brian54321uk wrote:
Hi
I would like to replace a string of characters in a file to the name of the directory it is in.
Thefore, in the example below, I would like to know how to replace "?"


open( F, $ARGV[0] );

while( <F> ) {

  s!abc123!?!;

  s!xyz123!123xyz!;


{

   print;

  }

}


Any help much appreciated.
Brian


See `perldoc File::Basename` for details.


#!/usr/bin/perl

use strict;
use warnings;

use File::Basename;

my $dir = dirname( $ARGV[0] );

open my $fh, '<', $ARGV[0] or die "cannot open $ARGV[0]: $!";

while( <$fh> ){
  s!abc123!$dir!g;
}continue{
  print;
}

close $fh or die "cannot close $ARGV[0]: $!";

__END__



Just tested this out and unfortunately instead of dirname replacing abc123, I get a .

Thanks
Brian


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


Reply via email to