remove all lines starting with b

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yawnmoth

    remove all lines starting with b

    Say I have the following file - dummy.txt:

    a 4treg
    a sdfas
    a 4egdsag
    b 1111
    a ergdsfg
    b 1111
    c waefasdf

    How would I go about removing all the lines that begin with a b? I
    thought the following script (which I wrote with the intention of
    replacing all such lines with the empty string) would but apparently it
    doesn't:

    <?
    $contents = file_get_conten ts('dummy.txt') ;
    echo "\r\n";
    echo preg_replace('/^b.*?\n/','',$contents) ;
    ?>

    Any ideas?

  • Rex

    #2
    Re: remove all lines starting with b

    I believe you want to read the file line by line and then try and match
    your regex to each line. try:

    $handle = @fopen("dummy.t xt", "r");
    if ($handle) {
    while (!feof($handle) ) {
    $buffer = fgets($handle);
    if( !preg_match('/^b.*?\n/',$buffer) )
    echo $buffer . "<br>";
    }
    fclose($handle) ;
    }

    Comment

    • d

      #3
      Re: remove all lines starting with b

      "Rex" <eric.fiori@gma il.com> wrote in message
      news:1143445129 .437173.181710@ t31g2000cwb.goo glegroups.com.. .[color=blue]
      >I believe you want to read the file line by line and then try and match
      > your regex to each line. try:
      >
      > $handle = @fopen("dummy.t xt", "r");
      > if ($handle) {
      > while (!feof($handle) ) {
      > $buffer = fgets($handle);
      > if( !preg_match('/^b.*?\n/',$buffer) )
      > echo $buffer . "<br>";
      > }
      > fclose($handle) ;
      > }[/color]

      You could use substr($buffer, 0, 1)=="b" as opposed to preg_match, as
      preg_match is a bit more intensive than just substr...


      Comment

      • Erwin Moller

        #4
        Re: remove all lines starting with b

        yawnmoth wrote:
        [color=blue]
        > Say I have the following file - dummy.txt:
        >
        > a 4treg
        > a sdfas
        > a 4egdsag
        > b 1111
        > a ergdsfg
        > b 1111
        > c waefasdf
        >
        > How would I go about removing all the lines that begin with a b? I
        > thought the following script (which I wrote with the intention of
        > replacing all such lines with the empty string) would but apparently it
        > doesn't:
        >
        > <?
        > $contents = file_get_conten ts('dummy.txt') ;
        > echo "\r\n";
        > echo preg_replace('/^b.*?\n/','',$contents) ;
        > ?>
        >
        > Any ideas?[/color]

        Hi,

        Read this:


        So you could add the m modifier, like this:
        echo preg_replace('/^b.*\\n/m','',$contents );

        where:
        ^b means begins with b
        ..* means: anything untill end of line (\n) is reached
        \\n means the end of line
        and the m outside the // means that your string is treated the way you ment
        too (see description at www.php.net).

        Regards,
        Erwin Moller

        Comment

        • David Haynes

          #5
          Re: remove all lines starting with b

          d wrote:[color=blue]
          > "Rex" <eric.fiori@gma il.com> wrote in message
          > news:1143445129 .437173.181710@ t31g2000cwb.goo glegroups.com.. .[color=green]
          >> I believe you want to read the file line by line and then try and match
          >> your regex to each line. try:
          >>
          >> $handle = @fopen("dummy.t xt", "r");
          >> if ($handle) {
          >> while (!feof($handle) ) {
          >> $buffer = fgets($handle);
          >> if( !preg_match('/^b.*?\n/',$buffer) )
          >> echo $buffer . "<br>";
          >> }
          >> fclose($handle) ;
          >> }[/color]
          >
          > You could use substr($buffer, 0, 1)=="b" as opposed to preg_match, as
          > preg_match is a bit more intensive than just substr...
          >
          >[/color]
          or just $buffer[0]...

          -david-

          Comment

          • Geoff Berrow

            #6
            Re: remove all lines starting with b

            Message-ID: <4427b828$0$110 73$e4fe514c@new s.xs4all.nl> from Erwin Moller
            contained the following:
            [color=blue]
            >Read this:
            >http://nl3.php.net/manual/en/referen....modifiers.php
            >
            >So you could add the m modifier, like this:
            >echo preg_replace('/^b.*\\n/m','',$contents );
            >
            >where:
            >^b means begins with b
            >.* means: anything untill end of line (\n) is reached
            >\\n means the end of line
            >and the m outside the // means that your string is treated the way you ment
            >too (see description at www.php.net).[/color]

            I really have to learn more about regex. In the meantime...

            <?
            $contents = file('dummy.txt ');
            foreach($conten ts as $value){
            echo ($value{0}!="b" )? $value."<br>" : "";
            }
            ?>

            --
            Geoff Berrow (put thecat out to email)
            It's only Usenet, no one dies.
            My opinions, not the committee's, mine.
            Simple RFDs http://www.ckdog.co.uk/rfdmaker/

            Comment

            • yawnmoth

              #7
              Re: remove all lines starting with b


              Erwin Moller wrote:[color=blue]
              > yawnmoth wrote:
              >
              > <snip>
              >
              > Hi,
              >
              > Read this:
              > http://nl3.php.net/manual/en/referen....modifiers.php
              >
              > So you could add the m modifier, like this:
              > echo preg_replace('/^b.*\\n/m','',$contents );
              >
              > where:
              > ^b means begins with b
              > .* means: anything untill end of line (\n) is reached
              > \\n means the end of line
              > and the m outside the // means that your string is treated the way you ment
              > too (see description at www.php.net).[/color]

              Thanks for the suggestion - that helped!

              I tried modifyng it such that it'd delete all lines not begining with a
              b but am once again having some difficulty...

              First, here's the (new) script:

              <?
              $contents = file_get_conten ts('dummy.txt') ;
              echo "\r\n";
              echo preg_replace('/^(?!b).*?\n/m','',$contents );
              ?>

              The (?!b) should, as I understand it, match everything but b. What
              instead seems to be happening is that, when using the dummy.txt file
              that was posted earlier, all lines begining with a are deleted, but the
              lines with c are still there.

              Now, I realize that in this example, I could just as effectively
              replace (?!b) with [^b] but this approach fails if b is anything other
              than a single character. (eg. if I wanted to replace b with
              192.168.1.1, I couldn't use [^...] - I'd have to use something like
              (?!b))

              Comment

              • yawnmoth

                #8
                Re: remove all lines starting with b


                yawnmoth wrote:[color=blue]
                > Erwin Moller wrote:[color=green]
                > > yawnmoth wrote:[/color]
                > <snip>
                >
                > Thanks for the suggestion - that helped!
                >
                > I tried modifyng it such that it'd delete all lines not begining with a
                > b but am once again having some difficulty...
                >
                > First, here's the (new) script:
                >
                > <?
                > $contents = file_get_conten ts('dummy.txt') ;
                > echo "\r\n";
                > echo preg_replace('/^(?!b).*?\n/m','',$contents );
                > ?>
                >
                > The (?!b) should, as I understand it, match everything but b. What
                > instead seems to be happening is that, when using the dummy.txt file
                > that was posted earlier, all lines begining with a are deleted, but the
                > lines with c are still there.
                >
                > Now, I realize that in this example, I could just as effectively
                > replace (?!b) with [^b] but this approach fails if b is anything other
                > than a single character. (eg. if I wanted to replace b with
                > 192.168.1.1, I couldn't use [^...] - I'd have to use something like
                > (?!b))[/color]

                Sorry for posting, again. I just thought I should add that I'm trying
                to do this with regular expressions, specifically, in an attempt to
                better my understanding of them. Using another approach that doesn't
                involve regular expressions would get the job done but would leave me
                still kinda clueless as to why (?!b) doesn't work.

                Comment

                • yawnmoth

                  #9
                  Re: remove all lines starting with b


                  yawnmoth wrote:[color=blue]
                  > Erwin Moller wrote:
                  > <snip>
                  >
                  > Thanks for the suggestion - that helped!
                  >
                  > I tried modifyng it such that it'd delete all lines not begining with a
                  > b but am once again having some difficulty...
                  >
                  > First, here's the (new) script:
                  >
                  > <?
                  > $contents = file_get_conten ts('dummy.txt') ;
                  > echo "\r\n";
                  > echo preg_replace('/^(?!b).*?\n/m','',$contents );
                  > ?>
                  >
                  > The (?!b) should, as I understand it, match everything but b. What
                  > instead seems to be happening is that, when using the dummy.txt file
                  > that was posted earlier, all lines begining with a are deleted, but the
                  > lines with c are still there.
                  >
                  > Now, I realize that in this example, I could just as effectively
                  > replace (?!b) with [^b] but this approach fails if b is anything other
                  > than a single character. (eg. if I wanted to replace b with
                  > 192.168.1.1, I couldn't use [^...] - I'd have to use something like
                  > (?!b))[/color]

                  Never mind - I got it working:

                  <?
                  $contents = file_get_conten ts('dummy.txt') ;
                  echo "\r\n";
                  echo preg_replace('/^(?!b).*?(?:\n| $)/m','',$contents );
                  ?>

                  The problem was due to the fact that the last line - which in dummy.txt
                  didn't start with a b - didn't end with a new line character but rather
                  with a EOF character (or maybe not a character at all? is EOF actually
                  a character?)

                  Comment

                  • Erwin Moller

                    #10
                    Re: remove all lines starting with b

                    Geoff Berrow wrote:
                    [color=blue]
                    > Message-ID: <4427b828$0$110 73$e4fe514c@new s.xs4all.nl> from Erwin Moller
                    > contained the following:
                    >[color=green]
                    >>Read this:
                    >>http://nl3.php.net/manual/en/referen....modifiers.php
                    >>
                    >>So you could add the m modifier, like this:
                    >>echo preg_replace('/^b.*\\n/m','',$contents );
                    >>
                    >>where:
                    >>^b means begins with b
                    >>.* means: anything untill end of line (\n) is reached
                    >>\\n means the end of line
                    >>and the m outside the // means that your string is treated the way you
                    >>ment too (see description at www.php.net).[/color]
                    >
                    > I really have to learn more about regex. In the meantime...
                    >
                    > <?
                    > $contents = file('dummy.txt ');
                    > foreach($conten ts as $value){
                    > echo ($value{0}!="b" )? $value."<br>" : "";
                    > }
                    > ?>[/color]

                    Personally I prefer non-regex solutions too.
                    Regex is ok as long as I am able to read what it says, and the bigger
                    complex regex always give me headaches. :P

                    Regards,
                    Erwin Moller

                    Comment

                    Working...