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

replace in OUTPUT file

Expand Messages
  • mablci
    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
    Message 1 of 5 , Apr 11, 2011
    • 0 Attachment
      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 $_;
      }
    • tianjun xu
      change open OUTFILE,
      Message 2 of 5 , Apr 11, 2011
      • 0 Attachment
        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]
      • merlyn@stonehenge.com
        ... mablci Hi I m trying to search and replace text in one file mablci and save it to a new file. I tried the code below but mablci it seems that I am just
        Message 3 of 5 , Apr 11, 2011
        • 0 Attachment
          >>>>> "mablci" == mablci <mablci@...> writes:

          mablci> Hi I'm trying to search and replace text in one file
          mablci> and save it to a new file. I tried the code below but
          mablci> it seems that I am just simply copying the INFILE to
          mablci> the OUTFILE. Any suggsetions on where I went wrong?

          Yes, you copied an exercise answer out of Learning Perl:
          (a) incorrectly
          (b) without reading the text

          Please read the text.

          --
          Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
          <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
          Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
          See http://methodsandmessages.posterous.com/ for Smalltalk discussion
        • Tim Lewis
          The arrow for your output is set to read instead of write. Change that line to: open OUTFILE, $output or die Can t open $output ($!) ; Tim
          Message 4 of 5 , Apr 12, 2011
          • 0 Attachment
            The arrow for your output is set to read instead of write. Change that line to:

            open OUTFILE, ">$output" or die "Can't open $output ($!)";


            Tim

            ---- mablci <mablci@...> wrote:
            > 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 $_;
            > }
            >
            >
          • mablci
            Thanks a lot. M
            Message 5 of 5 , Apr 13, 2011
            • 0 Attachment
              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.