Wednesday, February 26, 2014

Handy PERL regular expressions



Pattern

Description
/a./ The . matches any singe character except newline(\n)  eg.ab a
/[abcde]/

Matches a sting containing any one of the first five letters a lowercase alphabet eg.dog box
/[aeiouAEIOU]/ Matches any of the five vowels in either lower or uppercase eg.India egg
/[0123456789]/ Match if any single digit is present  eg. 0 3 41 100
/[0-9]/    Matches any single digit like the above                0 3
/[0-9\-]/ Match 0-9, or minus                        - 0 3
/[a-z0-9]/ Match any single lowercase letter or digit            a 0 9
/[a-zA-Z0-9_]/ Match any single letter, digit, or underscore            a X 9 _
/[^0-9]/ Match any single non-digit                    a
/[^aeiouAEIOU] Match any single non-vowel                    M N L
/[^\^]/ Match single character except an up-arrow            M N L
/[\da-fA-F]/ Match one hex digit
/fo+ba?r/ Matches an f followed by one or more o's followed by a b, followed by an optional a, followed by an r.
s/x+/boom/ Always replaces all consecutive x's with boom (resulting in fred boom barney), rather than just one or two x's, even though a shorter set of x's would also match the same regular expression. ie. $_ = "fred xxxxxxxxxx barney";
/x{5,10}/ Matches five to ten x's
/x{5,}/ Means five or more in this case
/x{5}/  Means "exactly this many" (five x's).
/x{0,5}/ Matches five or less x's
/a.{5}b/ Matches the letter a separated from the letter b by any five non-newline characters at any point in the string.
/a.*?c.*d/ matches the fewest characters between the a and c, not the most characters.
/oracle/ True if oracle matches. Note this will also match XoracleY
/\boracle\b/ True if only oracle matches
/ora\b/    True for anything that ends with ora, but fails for oracle. The \b is the word boundary.
/\bo/ True for oracle oozie operator, basically anything that begins with o
/\bora/ True only for anything that begins with ora
/^ora/ True only if it begins with ora, orc will fail
/^(o|h)/ True for only words begining with o or h
/^x|y/ Matches x at the beginning of line, or y anywhere
/abc*/ Matches ab, abc, abcc, abccc, abcccc, cab, zabc
/a|bc|d/ Matches a, bc, d, but not xyz, xbz
/(a|b)(c|d)/ Matches ac, ad, bc, or bd
/(song|blue)bird/ Matches songbird or bluebird
if (<STDIN> =~ /^y/i) True if the input begin with a y
s/foo/bar/g Replaces foo with bar in the string $_

Tuesday, February 11, 2014

Handy Linux and AIX system information




Linux

AIX
Number of Processors Number of  Physical CPU sockets
# cat /proc/cpuinfo  | grep "physical id" | sort | uniq | wc -l

Number of Cores
# cat /proc/cpuinfo | egrep "core id|physical id" | tr -d "\n" | sed s/physical/\\nphysical/g | grep -v ^$ | sort | uniq | wc -l

Total number of hyperthreads
# cat /proc/cpuinfo | grep processor | wc -l
prtconf | grep "Number of Processors"
Processor Type cat /proc/cpuinfo | grep "model name" | uniq

prtconf | grep "Processor Type"
Processor Clock Speed cat /proc/cpuinfo | grep "model name" | uniq prtconf | grep "Processor Clock Speed"
Disk information fdisk -l lspv
Network Card lspci | grep Ethernet

#lsdev -Cc if
# lsdev -Cc adapter
# entstat -d en0


Memory cat /proc/meminfo | grep MemTotal prtconf  | grep "Memory Size"
IP Address hostname -i prtconf | grep "IP Address"
HBA
lspci | grep HBA
lsdev -Cc adapter | grep "FC Adapter"

A simple PERL script that will give system and OS information on a Linux box

#!/usr/bin/perl -w

#
# A simple PERL script that gives system information like Linux kernel version
# CPU, Memory, IP address, and HBA
#

#
# Function that returns Linux kernel information
#

sub os {
    open (MYOS, "uname -a|");

    while (<MYOS>) {
        $my_os = $_;
        chomp($my_os);
        @kernel_version = split(/\s+/, $my_os);
        print "OS Version: $kernel_version[0] $kernel_version[2]\n";
    }
    close (MYOS);
}

#
# Function that returns the RedHat release information
#

sub rhat {
    open (MYRH, "cat /etc/redhat-release|");

        while (<MYRH>) {
            $my_rh = $_;
            chomp($my_rh);
            print "RedHat Release Version: $my_rh\n\n";
        }
        close (MYRH);
}

#
# Function that returns number of CPU sockets on the machine
#

sub numcpu {
    open (MYPROC, "cat /proc/cpuinfo  | grep \"physical id\" | sort | uniq | wc -l |");

    while (<MYPROC>) {
        $num_cpu = $_;
        chomp($num_cpu);
        print "Number of CPU/Sockets: $num_cpu\n\n";
    }
    close (MYPROC);
}

#
# Function that returns the number of hardware threads in the processor
#

sub numhwthreads {
    open (MYHWTHREADS, "cat /proc/cpuinfo | grep processor | wc -l |");

    while (<MYHWTHREADS>) {
        $num_threads = $_;
        chomp($num_threads);
        print "Number of Hardware threads: $num_threads\n\n";
    }
    close (MYHWTHREADS);
}

#
# Function that returns the processor type.
#

sub proctype {
    open (MYPROCTYPE, "cat /proc/cpuinfo | grep \"model name\" | uniq |");

    while (<MYPROCTYPE>) {
        $num_cpu = $_;
        chomp($num_cpu);
        @proc_info = split(/:/,$num_cpu);
        print "Processor Type: $proc_info[1]\n\n";
    }
    close (MYPROCTYPE);
}

#
# Function that returns the Memory information
#

sub memory {
    open (MYMEMORY, "cat /proc/meminfo | grep MemTotal |");

    while (<MYMEMORY>) {
        $my_memory = $_;
        chomp($my_memory);
        @memory_kb = split(/\s+/,$my_memory);
        $memory_gb = $memory_kb[1]/1048576;
        print "Memeory: $memory_gb GB\n\n";
    }
    close (MYMEMORY);
}

#
# Function that returns the IP address of the host
#

sub ipaddress {
    open (MYIP, "hostname -i |");

    while (<MYIP>) {
        $my_ip = $_;
        chomp($my_ip);
        print "IP address: $my_ip\n\n";
    }
    close (MYMEMORY);
}

#
# Function that returns the HBA information on the host
#

sub hba {
    open (MYHBA, "lspci | grep HBA|");

    while (<MYHBA>) {
        $my_hba = $_;
        chomp($my_hba);
        print "HBA: $my_hba\n";
    }
    close (MYHBA);
}

os();
rhat();
numcpu();
numhwthreads();
proctype();
memory();
ipaddress();
hba();

Tuesday, February 4, 2014