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

perl substitution

Expand Messages
  • Scott Mayo
    It has been a while since I have done any perl scripting. I remember having this problem before and I am not sure if I actually figured it out or not. If I
    Message 1 of 5 , Jul 2 9:01 AM
    • 0 Attachment
      It has been a while since I have done any perl scripting. I remember
      having this problem before and I am not sure if I actually figured it
      out or not. If I have a line like the following:

      [the time is 1:59:00] [Scott] How are you? [Allie] Fine today.

      Is there someway to replace everything up through the first ']'?

      If I searched like this s/^\[.*\]// that would find everything from the
      beginning up the ']' following Allie. I just want to replace the first
      occurance so the final output would be:

      [Scott] How are you? [Allie] Fine today.

      I am not sure how to tell it to stop once it has found the first occurance.

      Thanks.

      --
      Scott Mayo
      System Administrator
      Bloomfield Schools

      Duct tape is like the force, it has a light side and a dark side and it
      holds the universe together.
    • Christian Wessel
      Hello Scott, just try it with s/^ [.* ] [/ [/ Thats well defined. mfg/br, christian IT Systembetreuer/Geschäftsfeld PCO Informationstechnologie Continental
      Message 2 of 5 , Jul 2 9:22 AM
      • 0 Attachment
        Hello Scott,

        just try it with s/^\[.*\] \[/\[/

        Thats well defined.


        mfg/br, christian

        IT Systembetreuer/Geschäftsfeld PCO
        Informationstechnologie

        Continental AG, Plant Stöcken
        Jädekamp 30, 30419 Hannover, Germany
        Mob.: +49 175 930 14 48
        Tel.: +49 511 976 4271
        Fax : +49 511 976 8 4271
        eMail: christian.wessel@...
        Internet: http://www.conti-online.com
        ----------------------------------------------------
        Continental Aktiengesellschaft, Postfach/Postbox 1 69, D-30001 Hannover
        Vorsitzender des Aufsichtsrats/Chairman of the Supervisory Board: Dr.
        Hubertus von Grünberg
        Vorstand/Executive Board: Manfred Wennemer (Vorsitzender/Chairman), Dr.
        Alan Hippe, Gerhard Lerch, Dr. Karl-Thomas Neumann, Dr. Hans-Joachim
        Nikolin, Thomas Sattelberger, William L. Kozyra (stv./Deputy)
        Sitz der Gesellschaft/Registered Office: Hannover,
        Registergericht/Registered Court: Amtsgericht Hannover HRB 3527,
        USt.-ID-Nr./VAT-ID-No. DE 115645799
        ----------------------------------------------------
        Proprietary and confidential. Distribution only by express authority of
        Continental AG or its subsidiaries.




        Scott Mayo
        <sgmayo@...
        mfield.k12.mo.us> An
        Gesendet von: perl-beginner@yahoogroups.com
        perl-beginner@yah Kopie
        oogroups.com
        Thema
        [PBML] perl substitution
        02.07.2007 18:01


        Bitte antworten
        an
        perl-beginner@yah
        oogroups.com






        It has been a while since I have done any perl scripting. I remember
        having this problem before and I am not sure if I actually figured it
        out or not. If I have a line like the following:

        [the time is 1:59:00] [Scott] How are you? [Allie] Fine today.

        Is there someway to replace everything up through the first ']'?

        If I searched like this s/^\[.*\]// that would find everything from the
        beginning up the ']' following Allie. I just want to replace the first
        occurance so the final output would be:

        [Scott] How are you? [Allie] Fine today.

        I am not sure how to tell it to stop once it has found the first occurance.

        Thanks.

        --
        Scott Mayo
        System Administrator
        Bloomfield Schools

        Duct tape is like the force, it has a light side and a dark side and it
        holds the universe together.


        Unsubscribing info is here:
        http://help.yahoo.com/help/us/groups/groups-32.html
        Yahoo! Groups Links
      • Rob Biedenharn
        ... You could use a reluctant qualifier: s/^ [.*? ]// Or specifically look for non-]: s/^ [[^]]* ]// ...and if you want the space gone, too, add s* to the
        Message 3 of 5 , Jul 2 10:09 AM
        • 0 Attachment
          On Jul 2, 2007, at 12:01 PM, Scott Mayo wrote:

          > It has been a while since I have done any perl scripting. I remember
          > having this problem before and I am not sure if I actually figured it
          > out or not. If I have a line like the following:
          >
          > [the time is 1:59:00] [Scott] How are you? [Allie] Fine today.
          >
          > Is there someway to replace everything up through the first ']'?
          >
          > If I searched like this s/^\[.*\]// that would find everything from
          > the
          > beginning up the ']' following Allie. I just want to replace the
          > first
          > occurance so the final output would be:
          >
          > [Scott] How are you? [Allie] Fine today.
          >
          > I am not sure how to tell it to stop once it has found the first
          > occurance.
          >
          > Thanks.
          >
          > --
          > Scott Mayo
          > System Administrator
          > Bloomfield Schools
          >
          > Duct tape is like the force, it has a light side and a dark side
          > and it
          > holds the universe together.

          You could use a reluctant qualifier:

          s/^\[.*?\]//

          Or specifically look for non-]:

          s/^\[[^]]*\]//

          ...and if you want the space gone, too, add \s* to the end:

          s/^\[.*?\]\s*//
          s/^\[[^]]*\]\s*//

          -Rob

          Rob Biedenharn http://agileconsultingllc.com
          Rob@...
        • Scott Mayo
          I am going to ask this again since I don t think that I asked what I needed to the first time. I tried the suggestions, but they did not do what I wanted. I
          Message 4 of 5 , Jul 2 1:29 PM
          • 0 Attachment
            I am going to ask this again since I don't think that I asked what I
            needed to the first time. I tried the suggestions, but they did not do
            what I wanted. I do appreciate those helps though. I am going to parse
            a log file and here is what I basically want to do:

            I want to pull off the first bracketed info into a variable and leave
            the rest in another variable.

            Here are a couple of other things. There may be only one set of
            bracketed info and there may be none at all or there may be a lot of
            sets of bracketed info. So I could have the following in the file which
            is 3 lines in total:

            [the time is 1:59:00] [Scott] How are you? [Allie] Fine today.
            Thank you for coming.
            [Tell] Hey there.

            After parsing my lines I could have the following info.

            $line1[1] = [the time is 1:59:00]
            $line1[2] = [Scott] How are you? [Allie] Fine today.

            $line2[1] = Thank you for coming.

            $line3[1] = [Tell]
            $line3[2] = Hey there.

            Thanks again.

            --
            Scott Mayo
            System Administrator
            Bloomfield Schools


            Duct tape is like the force, it has a light side and a dark side and it
            holds the universe together.
          • Rob Biedenharn
            ... You need to use () s to capture the parts of the regular expression match that you re interested in keeping: For example: perl -w -e $line = [the time
            Message 5 of 5 , Jul 2 1:53 PM
            • 0 Attachment
              On Jul 2, 2007, at 4:29 PM, Scott Mayo wrote:
              > I am going to ask this again since I don't think that I asked what I
              > needed to the first time. I tried the suggestions, but they did
              > not do
              > what I wanted. I do appreciate those helps though. I am going to
              > parse
              > a log file and here is what I basically want to do:
              >
              > I want to pull off the first bracketed info into a variable and leave
              > the rest in another variable.

              You need to use ()'s to capture the parts of the regular expression
              match that you're interested in keeping:

              For example:

              perl -w -e '
              $line = "[the time is 1:59:00] [Scott] How are you? [Allie] Fine
              today.";
              $line =~ m/^(\[[^]]*\])?(.*)$/;
              print "bracketed part: $1\n";
              print "remaining line: $2\n";
              '

              bracketed part: [the time is 1:59:00]
              remaining line: [Scott] How are you? [Allie] Fine today.

              > Here are a couple of other things. There may be only one set of
              > bracketed info and there may be none at all or there may be a lot of
              > sets of bracketed info. So I could have the following in the file
              > which
              > is 3 lines in total:
              >
              > [the time is 1:59:00] [Scott] How are you? [Allie] Fine today.
              > Thank you for coming.
              > [Tell] Hey there.
              >
              > After parsing my lines I could have the following info.
              >
              > $line1[1] = [the time is 1:59:00]
              > $line1[2] = [Scott] How are you? [Allie] Fine today.
              >
              > $line2[1] = Thank you for coming.
              >
              > $line3[1] = [Tell]
              > $line3[2] = Hey there.
              >
              > Thanks again.
              >
              > --
              > Scott Mayo
              > System Administrator
              > Bloomfield Schools

              I very nearly gave you a variation of the standard "don't ask us to
              do your homework" when I noticed your .k12. address, but I decided to
              give you the benefit of the doubt when I got to your signature.

              The '?' in the regexp means that the first part is optional (i.e.,
              can match zero or one time).

              You might also have to check "if (defined $1)" to know whether there
              was a "first part" or not.

              -Rob

              Rob Biedenharn http://agileconsultingllc.com
              Rob@...
            Your message has been successfully submitted and would be delivered to recipients shortly.