Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

grab data records with fixed number of lines 1

Status
Not open for further replies.

LinguaFranca

Technical User
Jan 4, 2005
18
NL
I already serached the forum and asked other colleagues who work with Perl how to tackle my problem but they couldn't help me. I need to get filtered out all records that contain 5 lines. Records are seperated by a "blankline" (^$). Below is an example of the file. I would need the following record of the sample file below:
[COLOR=red yellow]
@6 String "cc_column_description" in StringTable.NET "UserResources"
@ Test comment
Description
=
Description
[/color red yellow]

as output. Can you help or point me to a location where I can find more information?

Thanks in advance.

sample file:
Code:
@ PASSOLO Text Translation File
@ enu -> eng
@ Translation List - UserResources:English (United Kingdom)
@CodePage1 1252
@CodePage2 1252
@ Don't modify lines starting with '@'!
@ For translation change lines after "=" Line
@ID 54355661

@25  String "cc_copy_function" in StringTable.NET "UserResources"
Copy Function
=
Copy Function

@6  String "cc_column_description" in StringTable.NET "UserResources"
@ Test comment
Description
=
Description

@27  String "cc_edit" in StringTable.NET "UserResources"----
Edit
=
Edit
 
look at setting your input record seperator as two newlines (ie one blankline), and read in the file like that.
Code:
$|="\n\n";
open FILE, "<PASSOLO.TXT";
while (<FILE>) {
  @lines=split (/\n/, $_):
  if ($#lines > 5) {
    #six lines (started from 0)
  } else {
    #do whatever
  }
}
close FILE;

Not sure about $| though, it's the input record seperator

HTH
--Paul

}


Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Changing the input record separator is probably the way to go, but - here's another way.
Code:
open LOG, "< logfile.txt" or die;

# Skip lines until the first record
do {
    $_ = <LOG>
} until (/@\d+/);

while (! eof(LOG)) {
    if (/@\d+/) {
        my @record;
        do {
            push @record, $_;
        } until (($_ = <LOG> || "") =~ /^\s*$/);
        print @record;
    }
    $_ = <LOG>;
}
close LOG;
 
Thanks for your help. You put me on the right track. This did the job:

Code:
#!usr/bin/perl

use locale;
use strict;
use warnings;

my $file=<@ARGV>;

open LOG, "< $file" or die;

# Skip lines until the first record
do {
    $_ = <LOG>
} until (/@\d+/);

while (! eof(LOG)) {
    if (/@[^\d]/) {
        my @record;
        do {
            push @record, $_;
        } until (($_ = <LOG> || "") =~ /^\s*$/);
        print "$file\t@record\n";
    }
    $_ = <LOG>;
}
close LOG;
 
Thanks for the star.

Hey Tojan, who's this rhash guy you're referring to? [ponder]
 
Paul said:
Not sure about $| though, it's the input record seperator

maybe you meant to say: it's not the input record seperator". $| is the command to flush the buffers.
 
What he^ said :)

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top