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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting info from text files

Status
Not open for further replies.

Saatchi

Technical User
Aug 13, 2002
4
GB
Being new to PERL, I'd like a bit of help writing a script to extract some data from a daily produced Textual Report file. The report file conatins the following data:-

Report 5: CPU Availability

Heading 1
Heading 2

Hour Box1 Box2 Box3

01:00 100.00 39.35 100.00 24.14 100.00 19.00
02:00 100.00 38.27 100.00 29.19 100.00 24.09
etc.......

I need to search through the text file to "Report 5", as it contains many other reports, and then extract the hourly data into the following format:-

Rep5,01,100.00,39.35,100.00,24.14,100.00,19.00
Rep5,02,100.00,38.27,100.00,29.19,100.00,24.09

where fields are:-

reportnum,hour,box1dataa,box1datab,box2dataa,box2datab,box3dataa,box3datab

Which I will then feed into a MySQL DB for analysis & graphical reporting.

Unfortunately I cannot guarantee that the lines I need will always be in the same place in the text file.

Can anyone help me please.
 
I'm not sure what exactly you mean by 'I cannot guarantee that the lines I need will always be in the same place in the text file'. How might the format be different from the example you posted?

but this is pretty flexible and works for the sample data you posted.
Code:
my @rep5 = ();
while (<DATA>)  {

    next unless /Report 5/;
    while (<DATA>) {

        last if /Report [^5]/;
        next unless /^\d/;
        my @line = split;
        $line[0] =~ s/:\d+//;
        push @rep5, join (',', 'Rep5', @line);
    }
}
# print &quot;$_\n&quot; for @rep5;

jaa
 
Thanks 'jaa'. This did the trick, although had to replace the print statement with:-

foreach $item (@rep5) {
print &quot;$item\n&quot; ;

(yes I did remove the '#')

Could not figure out how your print statement worked.

Also could you explain what benefit the 'my' function you used provides?

Cheers and thanks again.
Steve
 
This
Code:
print &quot;$_\n&quot; for @rep5;
is a compact way of writing
Code:
for ( @rep5 ) {  # Or foreach, doesn't matter

    print &quot;$_\n&quot;;

}
which is equivalent to
Code:
foreach $_ ( @rep5 ) { # because $_ gets set as the default if no loop variable is given.

    print &quot;$_\n&quot;;  
}
which is the same as your loop substituting $item for $_.

The [tt]my[/tt] makes the variable that follows it lexically confined to the enclosing block. So the [tt]my @rep5[/tt] gives [tt]@rep5[/tt] file scope (because there is no block that encloses is except for the file itself), whereas [tt]my @line[/tt] is local to the inner while loop, it doesn't exist outside of it.

I run most scripts using the [tt]strict[/tt] pragma which requires that all variables be declared, using [tt]my[/tt] or [tt]our[/tt].
There was no other specific reason for their use in the above code than that.

jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top