<?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>perl | /var/logs/paulooi.log</title>
	<atom:link href="https://logs.paulooi.com/tag/perl/feed" rel="self" type="application/rss+xml" />
	<link>https://logs.paulooi.com</link>
	<description>Systems Admin, Web Development and etc</description>
	<lastBuildDate>Tue, 13 Jul 2010 01:06:47 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>Perl, Trim White Space from a String</title>
		<link>https://logs.paulooi.com/perl-trim-white-space-from-a-string.php</link>
					<comments>https://logs.paulooi.com/perl-trim-white-space-from-a-string.php#comments</comments>
		
		<dc:creator><![CDATA[Paul Ooi]]></dc:creator>
		<pubDate>Tue, 13 Jul 2010 01:06:47 +0000</pubDate>
				<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[<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....</p>
The post <a href="https://logs.paulooi.com/perl-trim-white-space-from-a-string.php">Perl, Trim White Space from a String</a> first appeared on <a href="https://logs.paulooi.com">/var/logs/paulooi.log</a>.]]></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>The post <a href="https://logs.paulooi.com/perl-trim-white-space-from-a-string.php">Perl, Trim White Space from a String</a> first appeared on <a href="https://logs.paulooi.com">/var/logs/paulooi.log</a>.]]></content:encoded>
					
					<wfw:commentRss>https://logs.paulooi.com/perl-trim-white-space-from-a-string.php/feed</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Open file and Splitting String in Perl</title>
		<link>https://logs.paulooi.com/splitting-string-in-perl.php</link>
					<comments>https://logs.paulooi.com/splitting-string-in-perl.php#respond</comments>
		
		<dc:creator><![CDATA[Paul Ooi]]></dc:creator>
		<pubDate>Wed, 20 Jan 2010 14:35:38 +0000</pubDate>
				<category><![CDATA[Systems]]></category>
		<category><![CDATA[ironman]]></category>
		<category><![CDATA[perl]]></category>
		<guid isPermaLink="false">http://systems.takizo.com/?p=828</guid>

					<description><![CDATA[<p>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...</p>
The post <a href="https://logs.paulooi.com/splitting-string-in-perl.php">Open file and Splitting String in Perl</a> first appeared on <a href="https://logs.paulooi.com">/var/logs/paulooi.log</a>.]]></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>The post <a href="https://logs.paulooi.com/splitting-string-in-perl.php">Open file and Splitting String in Perl</a> first appeared on <a href="https://logs.paulooi.com">/var/logs/paulooi.log</a>.]]></content:encoded>
					
					<wfw:commentRss>https://logs.paulooi.com/splitting-string-in-perl.php/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
