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

using perl single quote

Expand Messages
  • Noah
    Hi there, I am using a variable $remove_ap as part of a search pattern. I am finding that the .* is not being used by the search statement. when I remove
    Message 1 of 6 , Aug 26, 2006
      Hi there,

      I am using a variable $remove_ap as part of a search pattern. I am
      finding that the .* is not being used by the search statement. when I
      remove .*} from the program I am able to search and replace the string.

      $remove_ap = 'policy-statement\sap-[0-9]+\s{.*}';
      while (<OLD>) {
      s/$remove_ap//g;
      print NEW $_;
      }


      any clues how I can get $remove_ap properly defined and used for the
      search statement?

      Cheers,

      Noah



      [Non-text portions of this message have been removed]
    • merlyn@stonehenge.com
      ... Noah Hi there, Noah I am using a variable $remove_ap as part of a search pattern. I am Noah finding that the .* is not being used by the search
      Message 2 of 6 , Aug 26, 2006
        >>>>> "Noah" == Noah <admin2@...> writes:

        Noah> Hi there,
        Noah> I am using a variable $remove_ap as part of a search pattern. I am
        Noah> finding that the .* is not being used by the search statement. when I
        Noah> remove .*} from the program I am able to search and replace the string.

        Noah> $remove_ap = 'policy-statement\sap-[0-9]+\s{.*}';
        Noah> while (<OLD>) {
        Noah> s/$remove_ap//g;
        Noah> print NEW $_;
        Noah> }

        Curly braces are significant in a regex. You'll need to quote them if you
        want them to be matched literally.

        Also, instead of a single-quote string, use a regex quote:

        my $pattern = qr/policy-statement\sap-\d+\s\{.*\}/;
        while (<OLD>) {
        s/$pattern//g;
        print NEW $_;
        }

        --
        Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
        <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
        Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
        See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
      • Noah
        ... Hi thanks for sending me on the right path. I am having a bit of trouble. I am finding there is an /n between the { and the } and the .* is not
        Message 3 of 6 , Aug 26, 2006
          Randal L. Schwartz wrote:
          >>>>>> "Noah" == Noah <admin2@...> writes:
          >>>>>>
          >
          > Noah> Hi there,
          > Noah> I am using a variable $remove_ap as part of a search pattern. I am
          > Noah> finding that the .* is not being used by the search statement. when I
          > Noah> remove .*} from the program I am able to search and replace the string.
          >
          > Noah> $remove_ap = 'policy-statement\sap-[0-9]+\s{.*}';
          > Noah> while (<OLD>) {
          > Noah> s/$remove_ap//g;
          > Noah> print NEW $_;
          > Noah> }
          >
          > Curly braces are significant in a regex. You'll need to quote them if you
          > want them to be matched literally.
          >
          > Also, instead of a single-quote string, use a regex quote:
          >
          > my $pattern = qr/policy-statement\sap-\d+\s\{.*\}/;
          > while (<OLD>) {
          > s/$pattern//g;
          > print NEW $_;
          > }
          >
          >
          Hi

          thanks for sending me on the right path. I am having a bit of trouble.
          I am finding there is an '/n' between the '\{' and the '\}' and the '.*'
          is not catching it. what is the regexp rule to include the \n as well?

          Cheers,

          Noah



          [Non-text portions of this message have been removed]
        • Chad Perrin
          ... If you add the s character at the end of your substitution regex, the . will include newline characters. -- CCD CopyWrite Chad Perrin [
          Message 4 of 6 , Aug 26, 2006
            On Sat, Aug 26, 2006 at 09:35:15PM -0700, Noah wrote:
            >
            > thanks for sending me on the right path. I am having a bit of trouble.
            > I am finding there is an '/n' between the '\{' and the '\}' and the '.*'
            > is not catching it. what is the regexp rule to include the \n as well?

            If you add the s character at the end of your substitution regex, the .
            will include newline characters.

            --
            CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
            "It's just incredible that a trillion-synapse computer could actually
            spend Saturday afternoon watching a football game." - Marvin Minsky
          • Noah
            ... so something still is not working for me. so I still must not be understanding something. so I have a huge text file that I am trying to strip certain
            Message 5 of 6 , Aug 26, 2006
              Chad Perrin wrote:
              > On Sat, Aug 26, 2006 at 09:35:15PM -0700, Noah wrote:
              >
              >> thanks for sending me on the right path. I am having a bit of trouble.
              >> I am finding there is an '/n' between the '\{' and the '\}' and the '.*'
              >> is not catching it. what is the regexp rule to include the \n as well?
              >>
              >
              > If you add the s character at the end of your substitution regex, the .
              > will include newline characters.
              >
              >

              so something still is not working for me. so I still must not be
              understanding something.
              so I have a huge text file that I am trying to strip certain lines.
              Below is a better look at my code. Here is an example fo the series of
              lines I am trying to search for:

              ---- searching for ----

              policy-statement ap-1 {
              term accept-term {
              from as-path path-1-accept;
              then accept;
              }
              term implicit-deny {
              then reject;
              }
              }

              ---- snip ---

              ---- code ----

              #!/usr/bin/perl

              undef $/;
              $remove_ap = qr/policy-statement\sap.*\}/;

              $input_file = "chi-c100-i2jOutput.txt";
              $output_file = "chi-c100-i2jOutput.mod.txt";

              my $input_config;
              if ( ! open OLD, "<$input_file" ) {
              die "Failed to open $input_file: $!";
              }
              open NEW, ">$output_file";

              while (<OLD>) {
              s/$remove_ap//gs;
              print NEW $_;
              }

              close(OLD);
              close(NEW);



              [Non-text portions of this message have been removed]
            • Damien Carbery
              ... trouble. ... the .* ... well? ... the . ... Your regex is designed for multiple lines but you are only reading one line at a time ( while ). Maybe
              Message 6 of 6 , Aug 29, 2006
                --- In perl-beginner@yahoogroups.com, Noah <admin2@...> wrote:
                >
                > Chad Perrin wrote:
                > > On Sat, Aug 26, 2006 at 09:35:15PM -0700, Noah wrote:
                > >
                > >> thanks for sending me on the right path. I am having a bit of
                trouble.
                > >> I am finding there is an '/n' between the '\{' and the '\}' and
                the '.*'
                > >> is not catching it. what is the regexp rule to include the \n as
                well?
                > >>
                > >
                > > If you add the s character at the end of your substitution regex,
                the .
                > > will include newline characters.
                > >
                > >
                >
                > so something still is not working for me. so I still must not be
                > understanding something.
                > so I have a huge text file that I am trying to strip certain lines.
                > Below is a better look at my code. Here is an example fo the series of
                > lines I am trying to search for:
                >
                > ---- searching for ----
                >
                > policy-statement ap-1 {
                > term accept-term {
                > from as-path path-1-accept;
                > then accept;
                > }
                > term implicit-deny {
                > then reject;
                > }
                > }
                >
                > ---- snip ---
                >
                > ---- code ----
                >
                > #!/usr/bin/perl
                >
                > undef $/;
                > $remove_ap = qr/policy-statement\sap.*\}/;
                >
                > $input_file = "chi-c100-i2jOutput.txt";
                > $output_file = "chi-c100-i2jOutput.mod.txt";
                >
                > my $input_config;
                > if ( ! open OLD, "<$input_file" ) {
                > die "Failed to open $input_file: $!";
                > }
                > open NEW, ">$output_file";
                >
                > while (<OLD>) {
                > s/$remove_ap//gs;
                > print NEW $_;
                > }
                >
                > close(OLD);
                > close(NEW);
                >

                Your regex is designed for multiple lines but you are only reading one
                line at a time ( while <OLD> ).

                Maybe read the entire file into a scalar variable (see perlfaq5, How
                can I read in an entire file all at once?)

                Change the 'while' loop to something like this:

                # Temporarily change $/ so that the entire file can be read in.
                my $var = do { local $/; <OLD> };
                # 'g' means repeat the substitution and 's' matches across lines.
                $var =~ s/$remove_ap//gs;
                # Only need to print it out once.
                print NEW $var;
              Your message has been successfully submitted and would be delivered to recipients shortly.