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

Help required in writing the perl script

Expand Messages
  • bpuri_12
    Hi, I am very new to Perl. Could someone please help me in writing the Following. I want to delete a line containing pattern from the file if it appears after
    Message 1 of 3 , Apr 7 4:29 AM
    • 0 Attachment
      Hi,

      I am very new to Perl.

      Could someone please help me in writing the Following.

      I want to delete a line containing pattern from the file if it appears after another specific pattern in the previous line

      e.g in the below file(lines are numbered for reference), I want to delete lines containing "null" if it appears after open.. so I want to delete line nos 4 and 10

      f1.txt
      1.abc
      2.test
      3.open
      4.xyz-null
      5.test
      6.null
      7.
      8.open
      9.
      10.null
      11.test


      Thanks in advance
    • andy_bach@wiwb.uscourts.gov
      Just to avoid the is this homework? harassment you might get, it s a good idea to post your attempt at this point. That keeps it from looking like you re
      Message 2 of 3 , Apr 7 9:30 AM
      • 0 Attachment
        Just to avoid the "is this homework?" harassment you might get, it's a
        good idea to post your attempt at this point. That keeps it from looking
        like you're just asking for somebody else to write your program entirely
        for you.

        Pedantically, you might look at the perldoc info that comes w/ perl
        $ perldoc perlintro

        ...
        Most text
        file processing can be done a line at a time with Perl's looping
        constructs.

        The "<>" operator is most often seen in a "while" loop:

        while (<INFILE>) { # assigns each line in turn to $_
        print "Just read in this line: $_";
        }
        ...

        Regular expressions

        Perl's regular expression support is both broad and deep, and is
        the subject of lengthy documentation in perlre-
        quick, perlretut, and elsewhere. However, in short:

        Simple matching
        if (/foo/) { ... } # true if $_ contains "foo"
        if ($a =~ /foo/) { ... } # true if $a contains "foo"


        a
        ----------------------
        Andy Bach
        Systems Mangler
        Internet: andy_bach@...
        Voice: (608) 261-5738;
        Cell: (608) 658-1890

        Any sufficiently advanced bug is indistinguishable
        from a feature. -- Rich Kulawiec

        [Non-text portions of this message have been removed]
      • Edward Willekens
        $open=0; if (open(FD,
        Message 3 of 3 , Apr 7 12:52 PM
        • 0 Attachment
          $open=0;
          if (open(FD,"<f1.txt")){
           while (<FD>) {
            if (/open/) {
             $open=1;
            }
            elsif (($open)&&(/null/)) {
             $open=0;
             next;
            }
            print $_."\n";
           }
           close(FD);
          }
            

          --- On Wed, 4/7/10, bpuri_12 <bpuri_12@...> wrote:

          From: bpuri_12 <bpuri_12@...>
          Subject: [PBML] Help required in writing the perl script
          To: perl-beginner@yahoogroups.com
          Date: Wednesday, April 7, 2010, 4:29 AM







           









          Hi,



          I am very new to Perl.



          Could someone please help me in writing the Following.



          I want to delete a line containing pattern from the file if it appears after another specific pattern in the previous line



          e.g in the below file(lines are numbered for reference), I want to delete lines containing "null" if it appears after open.. so I want to delete line nos 4 and 10



          f1.txt

          1.abc

          2.test

          3.open

          4.xyz-null

          5.test

          6.null

          7.

          8.open

          9.

          10.null

          11.test



          Thanks in advance

























          [Non-text portions of this message have been removed]
        Your message has been successfully submitted and would be delivered to recipients shortly.