Open file and Splitting String in Perl

What I gonna do is to read a bunch of data from a file, and perform some data processing from the string.

  • split() is the Perl function which I will use for filtering the data.
  • open for reading a file into array.

I took MRTG’s data log to do some data processing. On the first column of MRTG log file is Unix timestamp, I would like to print the unix Timestamp to human readable date and time, the other columns are traffic in and out in bytes, which I want to find out which traffic is more than certain values.

Below is the sample of MRTG’s log file (mrtg.log), it’s more than 1000 lines


1263954197 4778984843734474 1971197295597596
1263954197 184057628 54266478 184057628 54266478
1263953906 181938506 53715962 181938506 53715962
1263953700 182506581 53937120 182750043 54031903

Perl script will read the log data, split the data by space on each line into array, perform condition compare and find out the date.


#!/opt/local/bin/perl

open FILE, "mrtg.log";
my @logs = ;

foreach my $log (@logs)
{
        my @data = split(' ', $log);
        if ($data[1] > 500000000) {
            print scalar localtime($data[0]), " ## ", $data[1], " ## ", $data[0], "n";
        }
}

After going through more than 1000 lines of MRTG log, below is the output


Wed Jan 20 10:23:17 2010 ## 4778984843734474 ## 1263954197
Fri Jan 30 08:00:00 2009 ## 847491342 ## 1233273600
Tue Jan 13 08:00:00 2009 ## 844059530 ## 1231804800

This shows there were traffic spike twice during some where in Jan 2009. That’s split() and open in Perl.

Related posts:

  1. Perl, Trim White Space from a String When we grep a bunch of string, there are white...
  2. FreeBSD Port: Error Upgrading Perl 5.8.9 Usually I perform portupgrade daily on some of my FreeBSD...
  3. Modify or Adjust File Date Time on Unix/Linux You have folders or files in your server last modified...
  4. How to Remove MySQL Binary Log By default, MySQL 5.0 and above enables MySQL Binary Log....
  5. Quick File Copy on File Name with Sample Extension By default, some of the application installation provide you a...
  6. Bind Error: “max open files (3520) is smaller than max sockets (4096)” Just notice one of the DNS server has the error...
  7. Cacti Spine Source Installation Error on FreeBSD Just noticed FreeBSD’s port still using old version of spine,...
  8. Changing File’s Date and Time on Unix Systems Change Data and Time of a File At times, we...
  9. Configure Your Own screenrc file There are few shell scripts I need to run when...
  10. Malaysian Open Source Group Meet Up There are two exciting technologies taking the world by storm...

Tags: ,

Leave a Reply