<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>takizo, not takezo &#187; perl</title>
	<atom:link href="http://systems.takizo.com/tag/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://systems.takizo.com</link>
	<description>the systems admin blog</description>
	<lastBuildDate>Fri, 25 Nov 2011 00:55:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Perl, Trim White Space from a String</title>
		<link>http://systems.takizo.com/2010/07/13/perl-trim-white-space-from-a-string/</link>
		<comments>http://systems.takizo.com/2010/07/13/perl-trim-white-space-from-a-string/#comments</comments>
		<pubDate>Tue, 13 Jul 2010 01:06:47 +0000</pubDate>
		<dc:creator>takizo</dc:creator>
				<category><![CDATA[Systems]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[trim]]></category>

		<guid isPermaLink="false">http://systems.takizo.com/?p=1026</guid>
		<description><![CDATA[When we grep a bunch of string, there are white spaces where we wish to trim, especially the white space at the beginning and the end of a string. There is no trim function in Perl like trim in PHP. The Perl function below should help you to trim the string. #!/usr/bin/perl sub trim($); sub [...]]]></description>
			<content:encoded><![CDATA[<p>When we grep a bunch of string, there are white spaces where we wish to trim, especially the white space at the beginning and the end of a string. There is no trim function in Perl like trim in PHP. The Perl function below should help you to trim the string. </p>
<pre>
<code>
#!/usr/bin/perl

sub trim($);
sub ltrim($);
sub rtrim($);

sub trim($)
{
	my $trim_string = shift;
	$trim_string =~ s/^\s+//;
	$trim_string =~ s/\s+$//;
	return $trim_string;
}

sub ltrim($)
{
	my $trim_string = shift;
	$trim_string =~ s/^\s+//;
	return $trim_string;
}

sub rtrim($)
{
	my $trim_string = shift;
	$trim_string =~ s/\s+$//;
	return $trim_string;
}

my $long_string = "  \t  foo foo        bar \t \t bar          ";
print trim($long_string);
</code>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://systems.takizo.com/2010/07/13/perl-trim-white-space-from-a-string/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Open file and Splitting String in Perl</title>
		<link>http://systems.takizo.com/2010/01/20/splitting-string-in-perl/</link>
		<comments>http://systems.takizo.com/2010/01/20/splitting-string-in-perl/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 14:35:38 +0000</pubDate>
		<dc:creator>takizo</dc:creator>
				<category><![CDATA[Systems]]></category>
		<category><![CDATA[ironman]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://systems.takizo.com/?p=828</guid>
		<description><![CDATA[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&#8217;s data log to do some data processing. On the first [...]]]></description>
			<content:encoded><![CDATA[<p>What I gonna do is to read a bunch of data from a file, and perform some data processing from the string.</p>
<ul>
<li>split() is the Perl function which I will use for filtering the data. </li>
<li>open for reading a file into array.</li>
</ul>
<p>I took MRTG&#8217;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. </p>
<p><span id="more-828"></span></p>
<p>Below is the sample of MRTG&#8217;s log file (mrtg.log), it&#8217;s more than 1000 lines </p>
<pre>
<code>
1263954197 4778984843734474 1971197295597596
1263954197 184057628 54266478 184057628 54266478
1263953906 181938506 53715962 181938506 53715962
1263953700 182506581 53937120 182750043 54031903
</code>
</pre>
<p>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. </p>
<pre>
<code>
#!/opt/local/bin/perl

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

foreach my $log (@logs)
{
        my @data = split(' ', $log);
        if ($data[1] > 500000000) {
            print scalar localtime($data[0]), " ## ", $data[1], " ## ", $data[0], "\n";
        }
}
</code>
</pre>
<p>After going through more than 1000 lines of MRTG log, below is the output</p>
<pre>
<code>
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
</code>
</pre>
<p>This shows there were traffic spike twice during some where in Jan 2009. That&#8217;s split() and open in Perl.</p>
]]></content:encoded>
			<wfw:commentRss>http://systems.takizo.com/2010/01/20/splitting-string-in-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

