Month: October 2010

ptitle-particle1

Perl – Trim Text, Trim String

Perl – Trim Text, Trim String Left trim- ltrim or lstrip removes white spaces from the left side of a string: [perl] $str =~ s/^\s+//; [/perl] Right trim – rtrim or rstrip removes white spaces from the right side of a string: [perl] $str =~ s/\s+$//; [/perl] Trim Both [perl] $str =~ s/^\s+|\s+$//g [/perl] [perl] […]
Read more

Recursively Expand a NIS Netgroup

Recursively Expand a NIS Netgroup @results=expand_ng(); # Given a netgroup, will return a list of members. Will recurse # netgroups if the $recurse flag is set sub expand_ng { my($netgroup)=@_; my(@returnng); my($ng)=`ypmatch $netgroup netgroup`; $? != 0 and return; #Don't continue for duff netgroups my($value); $ng =~ s/\s+/ /g; #Compress whitespace $ng =~ s/\s+,/,/g; #Remove […]
Read more

RedHat vs Ubuntu – Useful Package Commands

RedHat vs Ubuntu – Useful Package Commands Task Red Hat/Fedora Ubuntu Refresh list of available packages Yum refreshes each time it’s used apt-get update Install a package from a repository yum install package_name apt-get install package_name Install a package file yum install package.rpm rpm -i package.rpm dpkg –install package.deb Remove a package rpm -e package_name […]
Read more

Readable Time – PHP, given a unix timestamp, return a readable time

Readable Time – PHP, given a UNIX timestamp, return a readable time # Given a unix timestamp, returns in readable time ie "1 day, 7 hours, 23 minutes, 2 seconds" function readable_time($timestamp, $num_times = 2) { //this returns human readable time when it was uploaded (array in seconds) $times = array(31536000 => 'year', 2592000 => […]
Read more

Add a timestamp to every STDOUT line

Add a timestamp to every STDOUT line I found this useful for debugging and just handy to have in the toolbox. Alias within your profile! Use: ./anyscript_you_choose | ./addtime [perl] #!/bin/perl -w # filename: ./addtime my($old)=select(STDOUT); $|=1; select(STDIN); $|=1; select ($old); while(<>) { my($sec,$min,$hour,$mday,$mon,$year,$wday,$ydat,$isdst)= localtime (time); printf “%02d:%02d:%4d %02d:%02d:%02d %s”,$mday,$mon+1,$year+1900,$hour,$min,$sec,$_; } [/perl]
Read more

Perl – Get the md5 hash for a file

Perl – Get the md5 hash for a file Get the MD5 hash for a file on the command line, using Digest::MD5 usage: ./md5sum.pl [filename] [-f] -f do not display filename [perl] use warnings; use strict; use Digest::MD5; sub md5sum { my $file = shift; my $digest = “”; eval{ open(FILE, $file) or die “Can’t […]
Read more

Get HTTP Code Response – Perl

Get HTTP Code Response – Perl Pass the script many hosts and obtain the HTTP response codes for the pages. [bash] ./http_check.pl … [/bash] [perl] use LWP::UserAgent; print get_url($url); sub get_url ($) { my $url = shift; my $ua = LWP::UserAgent->new; my $req = HTTP::Request->new(GET => $url); my $res = $ua->request($req); return $res->code; } ##Some […]
Read more

Select the next available value in a table

Select the next available value in a table Let’s say I have a table like this: ID Value ———————- 100 val1 101 val2 104 val3 105 val4 107 val5 [sql] SELECT Min(r1.uid) + 1 AS next_available_uid FROM unixuid AS r1 LEFT OUTER JOIN unixuid AS r2 ON r2.uid = r1.uid + 1 WHERE r1.uid > […]
Read more
Cart

No products in the basket.