hi everyone!

i'm kind of new to perl and having some problems with a script i wrote. hope i came to the wright place :)

here's my problem:
i wrote a script that goes through each line in a file (i loaded the file into an array "RevLogFile") and when it finds a match to a string it prints the line into a new file.

#!/usr/bin/perl
use warnings;

open (MYFILE, 'RevLogFile.txt');
@RevLogFile = <MYFILE>;
open (FILE, 'ParsedLog.txt');

foreach $line (@RevLogFile) {
               if ($line eq "Disk") {
                         open (FILE, '>>ParsedLog.txt');
                         print FILE $line;
                                           }
                                         }

the problem is, it doesnt work. running the script goes without problems, but when i check "ParsedLog.txt" i find a blank file.
i tried just printing each line to the file and it works, so the array and the 'foreach' works fine. i guess its the 'if' that's the problem.
any help appreciated! :)

it took me at least a day to try make this work before i came here. half an hour later and i found whats wrong:
if ($line eq "disk") - wrong
if ($line =~ m/disk/) - wright!! (=~ means "containing").

made a fool of myself...;)

Hi,
Two points to consider,
1. In your code, you are opening 'ParsedLog.txt' two times. One before entering for loop in R or read mode and second inside for loop in A or append mode.
Instead you can have this line, opening in W or A mode(you choose) before for loop, open (FILE, '>ParsedLog.txt'); # or open (FILE, '>>ParsedLog.txt') .

2. You never close the open handles,
So, close it after your job, close(FILE); close(MYFILE); .

You can learn from about regular expression by typing perldoc perlrequick for quick start or perldoc perlretut for tutorial.

katharnakh.

Hi katharnakh!

regarding your corrections:
you're wright, i got used to not closing my open handles. i haven't came a-cross any problems with that, but future wise i guess that's smarter. so thanx :)
about the twice opened handle - the first one supposed to be: open (FILE, '>ParsedLog.txt'); that way any time the script runs the file starts from scratch. i made a mistake copying the code, so my bad. but thanks anyway.

oh, and i have yet another problem.:icon_confused:
i have a script in which i enter a log file into an array and then i go line by line to search for a line containing "free disk space". once the line is found i want to check if the value is greater than 75.
the line looks like this:
free disk space = 79
i know how to do it in bash:
FDC=`$line | grep 'free disk space' | awk '{print $2}'`
if FDC > 75 blah blah blah

any idea how to do it in perl?
thanks all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.