You should really get one of the fine Perl books by O'reilly or spend at
least a little time learning Perl.  The eample below example will take a
user supplied regex pattern and search for those lines that contain the
regex and precede the line with a '#'.

Here is an example:

#!/usr/bin/perl -w
# This searches for a string and comments it out of the file.
use strict;

if ( $#ARGV < 0 ) {

  print "usage:  $0 pattern file1 file2 ...\n",
        "\nThis script will rewrite files with the specified line
commented with a #.\n";
  exit 1 ;

}

my $pat=shift(@ARGV);

print "Searching files @ARGV for pattern $pat\n";

# Begin the text searching

foreach my $file (@ARGV) {

  open (READ,"$file") or die "Can't open $file for reading:$!\n";
  my @newfile;

  while (<READ>) {
    chomp();
    if ( /$pat/ ) {
      # to remove the line from the new file
      # simply place a # in front of the line below
      push(@newfile,"\#$_");
    } else {
      push(@newfile,$_);
    }

  }
  
  close(READ);

  # Write the new file
  open(WRITE,">$file") or die"Can't open $file for writing:$!\n";
  foreach my $line (@newfile) {
    print WRITE "$line\n";
  }
  close(WRITE);
}

Hope this helps,

On Fri, 2003-03-07 at 14:16, [EMAIL PROTECTED] wrote:
> Hi perlians,
> 
> How can make to open a file and search on it some character to remove or
> comment. Sorry but I'am a newbie++newbiw about the fantastic language Perl
> and now for me it's very difficult approach an algorithm.
> 
> Does someone here can you help me with small example.
> Many thanks at all.
> 
> Marco    
-- 
Bryan DeLuca <[EMAIL PROTECTED]>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to