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

Change first occurrence of a string in multiple files

Expand Messages
  • Terry Fowler
    I copied and pasted the following into 40 html files:
    Message 1 of 6 , Nov 25, 2002
    • 0 Attachment
      I copied and pasted the following into 40 html files:

      <style type="text/css">
      <!--#if expr='${HTTP_USER_AGENT} = /.*MSIE.*/' -->
      <!--#include virtual="msie.css" -->
      <!--#else -->
      <!--#if expr='${HTTP_USER_AGENT} = /.*SunOS.*/' -->
      <!--#include virtual="suncmc.css" -->
      <!--#else -->
      <!--#include virtual="ns.css" -->
      <!--#endif -->
      </style>

      Then I slapped myself on the forehead when I realized
      that the first "else" needs to be "elsif". Well, I could
      have manually edited all 40 files in the amount of time
      I've spent hunting around in my codelibrary and the perl
      cookbook and searching the perl-beginner archive trying
      to find a Perl way. I got a suggestion for a one-liner
      but it changed BOTH occurrences of "else", not just the
      first occurrence.

      It seems to me that I've always heard that this
      is just the kind of thing Perl is best at but I'm just
      not getting anywhere. I can handle opening all
      the html files in pwd and such, but experimenting with
      just one file all I've managed so far is to change:

      <!--#include virtual="msie.css" -->
      <!--#else -->
      <!--#if expr='${HTTP_USER_AGENT} = /.*SunOS.*/' -->

      to look like this:

      <!--#include virtual="msie.css" -->
      <!--#else -->
      <!--#elsif -->
      <!--#if expr='${HTTP_USER_AGENT} = /.*SunOS.*/' -->

      I was going to include a code snippet but decided to
      spare y'all that pain. ;)

      Terry Fowler
      perl-beginner for life
    • Charles K. Clarkson
      ... Slap yourself on the head again. You only need one file like this to load from SSI. You could replace all of the above with:
      Message 2 of 6 , Nov 25, 2002
      • 0 Attachment
        Terry Fowler [quackamoe@...] wrote:

        : I copied and pasted the following into 40 html files:
        :
        : <style type="text/css">
        : <!--#if expr='${HTTP_USER_AGENT} = /.*MSIE.*/' -->
        : <!--#include virtual="msie.css" -->
        : <!--#else -->
        : <!--#if expr='${HTTP_USER_AGENT} = /.*SunOS.*/' -->
        : <!--#include virtual="suncmc.css" -->
        : <!--#else -->
        : <!--#include virtual="ns.css" -->
        : <!--#endif -->
        : </style>

        Slap yourself on the head again. You only
        need one file like this to load from SSI. You
        could replace all of the above with:

        <!--#include virtual="/style.shtml"-->

        Place everything above in the style.shtml
        file. Next time you need to change something it
        will be in one file. This script will probably
        do what you need. Test it on one file first. I
        don't use the -i option often enough to be
        confident in the results.


        #!/usr/local/bin/perl -i.orig

        while ( <> ) {
        if ( /<style[^>]/ .. m|</style>| ) {
        print qq|<!--#include virtual="/style.shtml"-->\n| if
        /<!--#endif -->/;
        } else {
        print;
        }
        }

        HTH,

        Charles K. Clarkson
        --
        Head Bottle Washer,
        Clarkson Energy Homes, Inc.
        Mobile Home Specialists
        254 968-8328
      • Denis ROUX
        Hello, I try to understand the mechanism for using a Module (ie Derivative) but I am blocked... What I would like is: to have the first and second derivative
        Message 3 of 6 , Nov 25, 2002
        • 0 Attachment
          Hello,
          I try to understand the mechanism for using a Module (ie Derivative) but
          I am blocked...
          What I would like is:
          to have the first and second derivative of the function Y(x) contained
          in the file data (the structure of my data file is given at the end of
          this mail). For that I import the data like shown behind...
          I try to used the Derivative Module but I have the answer:

          Use of uninitialized value in subtraction (-) at
          /usr/lib/perl5/site_perl/5.6.1/Math/Derivative.pm line 51, <DATA> line
          119

          If someone can give me some help I will be very happy

          Have a nice day

          Denis


          The script:
          ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          #!/usr/bin/perl -w

          use Math::Derivative qw(Derivative1 Derivative2);

          open (DATA,"data") or die "can't open datafile - le fichier n'est pas
          lisible: $!\n";
          LINE : while (my $ligne=<DATA>){
          if ($ligne=~/^#/){
          next LINE;
          };
          chop $ligne;
          my ($a, $b)=split(/,/, $ligne);
          push(my @x, $a);
          push(my @y, $b);
          };
          @dydx=Derivative1(\@x,\@y);
          @d2ydx2=Derivative2(\@x,\@y);
          @d2ydx2=Derivative2(\@x,\@y,$yp0,$ypn);
          close DATA;
          ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          end of the script



          shape of the data file:
          +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          # time (s)",x(mm),
          -0.2609,-1.09151,
          -0.26037,-1.08761,
          -0.25984,-1.08762,
          -0.25932,-1.11242,
          -0.25879,-1.12072,
          -0.25827,-1.12761,
          -0.25774,-1.16896,
          -0.25721,-1.17595,
          -0.25616,-1.22703,
          -0.25564,-1.27614,
          ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        • Charles K. Clarkson
          ... You should use strict . It will help you become a better programmer: use strict; ... Don t use that final n after $!. You ll get more complete
          Message 4 of 6 , Nov 25, 2002
          • 0 Attachment
            Denis ROUX [droux@...] scribbled:

            : Hello,
            : I try to understand the mechanism for using a Module
            : (ie Derivative) but I am blocked...
            : What I would like is:
            : to have the first and second derivative of the
            : function Y(x) contained in the file data (the
            : structure of my data file is given at the end
            : of this mail). For that I import the data like
            : shown behind... I tried to use the Derivative
            : Module but I have the answer:
            :
            : Use of uninitialized value in subtraction (-)
            : at /usr/lib/perl5/site_perl/5.6.1/Math/Derivative.pm
            : line 51, <DATA> line 119
            :

            : #!/usr/bin/perl -w

            You should use "strict". It will help you become
            a better programmer:

            use strict;

            : use Math::Derivative qw(Derivative1 Derivative2);
            :
            : open (DATA,"data") or die "can't open datafile - le fichier n'est pas
            : lisible: $!\n";

            Don't use that final "\n" after $!. You'll get
            more complete information without it.

            : LINE : while (my $ligne=<DATA>){
            : if ($ligne=~/^#/){
            : next LINE;
            : };
            : chop $ligne;

            Forget "chop" exists. "chomp" is much safer.

            : my ($a, $b)=split(/,/, $ligne);
            : push(my @x, $a);
            : push(my @y, $b);

            Here's your problem. Each time you loop through
            <DATA> you reset @x and @y. Better to declare them
            before the while block.

            : };

            While not an error, it is not necessary to use
            the closing semicolon on code blocks.

            : @dydx=Derivative1(\@x,\@y);
            : @d2ydx2=Derivative2(\@x,\@y);
            : @d2ydx2=Derivative2(\@x,\@y,$yp0,$ypn);
            : close DATA;

            Your indenting sucks. It took me way too long
            to see where the while block ended. You need to
            practice using "my" on all your variables. White
            space characters add legibility to your code. Use
            white space liberally.

            #!/usr/local/bin/perl

            use warnings;
            use strict;
            use Math::Derivative qw( Derivative1 Derivative2 );

            my( @x, @y );
            while ( <DATA> ) {
            next if /^#/;

            chomp( my( $a, $b ) = split /,/ );

            push @x, $a;
            push @y, $b;
            }

            my @dydx = Derivative1( \@x, \@y );
            my @d2ydx2 = Derivative2( \@x, \@y );
            my @d3ydx3 = Derivative2( \@x, \@y, my $yp0, my $ypn );


            __END__
            # time (s)",x(mm),
            -0.2609,-1.09151,
            -0.26037,-1.08761,
            -0.25984,-1.08762,
            -0.25932,-1.11242,
            -0.25879,-1.12072,
            -0.25827,-1.12761,
            -0.25774,-1.16896,
            -0.25721,-1.17595,
            -0.25616,-1.22703,
            -0.25564,-1.27614,


            HTH,

            Charles K. Clarkson
            --
            Head Bottle Washer,
            Clarkson Energy Homes, Inc.
            Mobile Home Specialists
            254 968-8328
          • Terry Fowler
            ... Hmm, I think this calls for more than one additional slap. :) ... Oh, it certainly did! Both the common sense point about putting the conditional SSI stuff
            Message 5 of 6 , Nov 26, 2002
            • 0 Attachment
              --- In perl-beginner@y..., "Charles K. Clarkson" <cclarkson@h...>
              wrote:
              >
              > Slap yourself on the head again. You only
              > need one file like this to load from SSI. You
              > could replace all of the above with:
              >
              > <!--#include virtual="/style.shtml"-->
              >
              Hmm, I think this calls for more than one additional slap. :)

              > Place everything above in the style.shtml
              > file. Next time you need to change something it
              > will be in one file. This script will probably
              > do what you need. Test it on one file first. I
              > don't use the -i option often enough to be
              > confident in the results.
              >
              >
              > #!/usr/local/bin/perl -i.orig
              >
              > while ( <> ) {
              > if ( /<style[^>]/ .. m|</style>| ) {
              > print qq|<!--#include virtual="/style.shtml"-->\n| if
              > /<!--#endif -->/;
              > } else {
              > print;
              > }
              > }
              >
              > HTH,

              Oh, it certainly did! Both the common sense point about putting
              the conditional SSI stuff in a file and calling it and the part
              about how to replace what I already head. The part about -i OTOH
              didn't help yet. I haven't quite figured that out so I just did
              a glob and a foreach to open each file and make the change. Here's
              what I ended up with. Comments welcome!

              #!/apps/LWperl/bin/perl -w

              use strict;

              my $infilename;
              my $outfilename;
              my @files = glob ("*html");
              my $file;

              foreach $file ( @files ) {

              $infilename = "$file";
              $outfilename = "$infilename.out";

              open(FILE, "<$infilename") or die "could not open file
              $infilename.$!\n";
              open(OUTFILE, ">$outfilename") or die "could not open file
              $outfilename.$!\n";

              while ( <FILE> ) {
              if ( /<style[^>]/ .. m|</style>| ) {
              print OUTFILE qq|<!--#include virtual="./style.html"-->\n| if
              /<!--#endif -->/;
              }
              else {
              print OUTFILE;
              }
              }

              close(FILE);
              close(OUTFILE);
              system ( "mv $outfilename $infilename" );
              }

              __END__


              Yes, I know that the shebang is odd; for whatever reason that's
              where our sysadmin put perl.
            • Denis ROUX
              Thanks to Charles K. Clarkson for his help. I will take in account his remarks. I hope that I can help someone when I will be stronger ... Have a good day
              Message 6 of 6 , Nov 26, 2002
              • 0 Attachment
                Thanks to Charles K. Clarkson for his help.

                I will take in account his remarks.

                I hope that I can help someone when I will be stronger ...

                Have a good day

                Denis ROUX

                > #!/usr/local/bin/perl
                >
                > use warnings;
                > use strict;
                > use Math::Derivative qw( Derivative1 Derivative2 );
                >
                > my( @x, @y );
                > while ( <DATA> ) {
                > next if /^#/;
                >
                > chomp( my( $a, $b ) = split /,/ );
                >
                > push @x, $a;
                > push @y, $b;
                > }
                >
                > my @dydx = Derivative1( \@x, \@y );
                > my @d2ydx2 = Derivative2( \@x, \@y );
                > my @d3ydx3 = Derivative2( \@x, \@y, my $yp0, my $ypn );
                >
                >
              Your message has been successfully submitted and would be delivered to recipients shortly.