Loading ...
Sorry, an error occurred while loading the content.
 

Re: replace in OUTPUT file

Expand Messages
  • mablci
    Thanks a lot. M
    Message 1 of 5 , Apr 13, 2011
      Thanks a lot.
      M

      --- In perl-beginner@yahoogroups.com, tianjun xu <tianjunx@...> wrote:
      >
      > change
      >
      > open OUTFILE, "<$output" or die "Can't open $output ($!)";
      >
      > to
      >
      > open OUTFILE, ">", $output" or die "Can't open $output ($!)";
      >
      >
      >
      > --- On Mon, 4/11/11, mablci <mablci@...> wrote:
      >
      > From: mablci <mablci@...>
      > Subject: [PBML] replace in OUTPUT file
      > To: perl-beginner@yahoogroups.com
      > Date: Monday, April 11, 2011, 4:43 PM
      >
      >
      >
      >
      >
      >
      >
      >  
      >
      >
      >
      >
      >
      >
      >
      >
      >
      > Hi I'm trying to search and replace text in one file
      >
      > and save it to a new file. I tried the code below but
      >
      > it seems that I am just simply copying the INFILE to
      >
      > the OUTFILE. Any suggsetions on where I went wrong?
      >
      > Thanks,
      >
      > M
      >
      >
      >
      > #!/usr/bin/perl -w
      >
      > use strict;
      >
      >
      >
      > my $input;
      >
      > my $output;
      >
      > my $search;
      >
      > my $replace;
      >
      >
      >
      > print "enter an input file name:\n";
      >
      > $input = <STDIN>;
      >
      > chomp($input);
      >
      >
      >
      > print "enter an output file name:\n";
      >
      > $output = <STDIN>;
      >
      > chomp($output);
      >
      >
      >
      >
      >
      > print "enter a search pattern:\n";
      >
      > $search = <STDIN>;
      >
      > chomp($search);
      >
      >
      >
      > print "enter a replacement string:\n";
      >
      > $replace = <STDIN>;
      >
      > chomp($replace);
      >
      >
      >
      > open INFILE, "<$input" or die "Can't open $input ($!)";
      >
      > open OUTFILE, "<$output" or die "Can't open $output ($!)";
      >
      >
      >
      > while (<INFILE>) {
      >
      > s/$search/$replace/g;
      >
      > print OUTFILE $_;
      >
      > }
      >
      >
      >
      >
      >
      >
      >
      >
      >
      >
      >
      >
      >
      >
      >
      >
      >
      >
      >
      >
      >
      >
      > [Non-text portions of this message have been removed]
      >
    Your message has been successfully submitted and would be delivered to recipients shortly.