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

Re: [PBML] perl substitution

Expand Messages
  • 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 1 of 5 , Jul 2, 2007
    • 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 2 of 5 , Jul 2, 2007
      • 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 3 of 5 , Jul 2, 2007
        • 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.