SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    print the values

    Code:
    #!/usr/bin/perl
    while (<DATA>) {
    
    
     if (/\{TT\}/ .. /^\{TAG\}/)
                    {
                    unless (/^\{(TT|TAG)\}/)
                            {
                            $deletestrings = $_;
                            print $deletestrings;
             #      $_ = '' if index( $_, "$deletestrings" ) >= 0;
                    }
            }
    
    }
    __DATA__
    S 9912290449 00005941^B{TT}
    D
    {TAG}
    9912290449
    {PUBLICATION}
    THE OS
    {DATE}
    000101
    
    S 9912290450 00005941^B{TT}
    R
    {TAG}
    9912290450
    {DATE}
    000101
    {TDATE}
    Saturday, January 1, 2000
    
    
    S 9912290451 00005941^B{TT}
    D
    {TAG}
    9912290451
    {DATE}
    000101
    {TDATE}
    Saturday, January 1, 2000



    When I print the output is
    Code:
    S 9912290449 00005941^B{TT}
    D
    S 9912290450 00005941^B{TT}
    R
    S 9912290451 00005941^B{TT}
    D
    How to print only the lines which has D and it's above line.
    Code:
    S 9912290449 00005941^B{TT}
    D
    S 9912290451 00005941^B{TT}
    D

  2. #2
    SitePoint Zealot Bompa's Avatar
    Join Date
    Feb 2008
    Posts
    179
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How to print only the lines which has D and it's above line.
    Code:

    S 9912290449 00005941^B{TT}
    D
    S 9912290451 00005941^B{TT}
    D

    You want to print only the lines where the preceding line begins with a 'D'?

    Is that what you want?

  3. #3
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code Perl:
    use strict;
     
    open DATA, './data.txt';
    my @lines = <DATA>;
    close DATA;
     
    for my $line_nr (0..$#lines) {
     
        if ($lines[$line_nr] =~ m/^D\s*$/) {
            print "$lines[$line_nr- 1]$lines[$line_nr]";
        }
    }
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •