Thursday, April 24, 2014

PERL Training

PERL TRAINING
---------------------------------------------
Day1: Data Structures and Inbuilt functions
Day2: Complex data structures & Perl modules
Day3: Object oriented Perl & DBI, CGI, Network, Process, Threads

*******************
DAY 1
*******************
PERL: Practical extraction and reporing language

Most of the features are derived from C, Shell, awk, sed and C++

--> perl -v ------------------> Shows the version of the perl which is installed
--> perl -V -----------------> Shows the location of the perl libraries

----------------------------------------------
Scalar variable:

Prefix of the variable will denote the type of the variable.
ex: $a ---> its a scalar variable
$name= 'arun';
$value=2.5;
**Every variable in PERL is initialized.
**By defalut the variable is initialized to "UNDEF". undef does not has any type, that is neither string, or int or float.
RESETING A VARIABLE
$a=10;
$a=undef;
or
undef($a);

**Same variable name can be used to denote different datatypes
Ex: $a ---------> scalar variable
@a ---------> array
%a ---------> Hash

**User defined variables start with alphabets;
**inbuilt variables will start with num/special character
==================================================
Program to read the input from user:
==================================================
#! /usr/bin/perl

package main;

$a=<STDIN>;
$b=<STDIN>;

$res=$a+$b;
print "' Result = $res";

======================================================================== Program to print the environment variable or shell variable in PERL Program
========================================================================
#!/usr/bin/perl

package main;

print "Printing environment variable";

print "value = $ENV{PATH}";

OUTPUT:

[ngdbprod@ngdb Vishwa_Perl]$ perl 3.pl
Printing environment variablevalue = /usr/lib/jvm/java-1.7.0-openjdk.x86_64/bin:/Softwares/ant/apache-ant-1.8.2/bin:/opt/svn/bin:/opt/maven/apache-maven-3.0.4/bin:/usr/lib64/qt-3.3/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/ngdbprod/bin[ngdbprod@ngdb Vishwa_Perl]$

======================================================================== PROGRAM TO PRINT THE VARIABLE IN BINARY VARIABLE (using type specifier ex:%d, %b, %f etc)
========================================================================
#! /usr/bin/perl

package main;

$a=20;
print "TO PRINT THE VARIABLE IN BINARY\n";
printf("' Result %b",  $a);

OUTPUT:
[ngdbprod@ngdb Vishwa_Perl]$ perl 4.pl
TO PRINT THE VARIABLE IN BINARY
' Result 10100
[ngdbprod@ngdb Vishwa_Perl]$
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
=====================================================================================================
PROGRAM TO PRINT THE VARIABLE IN BINARY VARIABLE (using type specifier ex:%d, %b, %f etc)
=====================================================================================================



@@@@@@@@@@@@@@@@@@@@@@@@@
PERL DOCUMENTATION
@@@@@@@@@@@@@@@@@@@@@@@@@

**Its help on any inbuilt functions
**Perl documentation is the utility available in PERL to understand the documentation of the default inbuilt functions available in PERL

ex:    perldoc -f <function name>
perldoc -f print
perldoc -f chomp

**To get the help on the inbuilt variables
ex:    perldoc -v $,;
perldoc -v $a

**To recursive search for a word in PERL DOCUMENTATION
ex:   perldoc -r "word"

**To Query the perl FAQs
ex:   perldoc -q date
       perldoc -q socket

** To get the help on perl library
ex:    perldoc List::<Library>
perldoc List::Util

SYNOPSIS
           use List::Util qw(first max maxstr min minstr reduce shuffle sum);

EX:
Its similar to #include of the C programming
#include<math.h>
use List::Util qw(sum)
================================================================================
PROGRAM TO USE PERL LIBRARY UTILITIES
================================================================================
#! /usr/bin/perl

package main;

use List::Util qw(sum);

print sum(10,20.30);
print"\n";

OUTPUT
[ngdbprod@ngdb Vishwa_Perl]$ perl 5.pl
30.3
[ngdbprod@ngdb Vishwa_Perl]$
================================================================================
PROGRAM TO USE MULTIPLE PERL LIBRARY UTILITIES IN A SINGLE PERL PROGRAM
================================================================================
#! /usr/bin/perl

package main;

use List::Util qw(sum max);

print sum(10,20,30);
print"\n";

print max(10,20,40),"\n";

[ngdbprod@ngdb Vishwa_Perl]$

OUTPUT
[ngdbprod@ngdb Vishwa_Perl]$ perl 5.pl
60
40
[ngdbprod@ngdb Vishwa_Perl]$

** TO LIST ALL THE PERLDOC FOR THE INBUILT FUNCTIONS
ex:
perldoc perlfunc
** TO LIST ALL THE PERLDOC FOR THE INBUILT VARIABLES
ex:
perldoc perlvar
** TO LIST ALL THE PERLDOC FOR THE INBUILT OPERATORS
ex:
perldoc perlop
** TO LIST ALL THE PERLDOC MAJOR FEATURES
ex:
perldoc perlcheat
** TO LIST ALL THE PERLDOC FOR THE INBUILT TUTORIALS
ex:
perldoc perl


################################
SCALAR INBUILT FUNCTIONS
################################

$name='arun';
$res=uc($name);  -->converts to uppercase
$res=lc($name); -->Converts to lowercase
$n=length($name); -->Length of the string
$n=index($name,'u');         -->Search for the index of the char in the word
$n=rindex($name,'u'); -->Search for hte index of the char in the word from the rightside
$res=substr($name,0,3);   -->Return the characters from 0 till 3rd char
$res=reverse($name); -->Reverse of the name

==================================================
PROGRAM
==================================================
[ngdbprod@ngdb Vishwa_Perl]$ cat 6.pl
$name='arun';
        $res=uc($name);
print"$res\n";
        $res=lc($name);
print"$res\n";
        $n=length($name);
print"$n\n";
        $n=index($name,'u');
print"$n\n";
        $n=rindex($name,'u');
print"$n\n";
        $res=substr($name,0,3);
print"$res\n";
        $res=reverse($name);
print"$res\n";
[ngdbprod@ngdb Vishwa_Perl]$

OUTPUT

[ngdbprod@ngdb Vishwa_Perl]$ perl 6.pl
ARUN
arun
4
2
2
aru
nura
[ngdbprod@ngdb Vishwa_Perl]$


================================================================
CHOMP FUNCTION
chomp is a special function which deletes the trailing new line character from the input
==================================================================

[ngdbprod@ngdb Vishwa_Perl]$ cat 7.pl
chomp($str1=<STDIN>);
chomp($str2=<STDIN>);
$str3=$str1.$str2;

print "Concatinated String is $str3";

OUTPUT
hari
prasad
Concatinated String is hari prasad

[ngdbprod@ngdb Vishwa_Perl]$

========================================================================
PROGRAM TO DEMO STRING MANUPULATION
========================================================================

[ngdbprod@ngdb Vishwa_Perl]$ cat 8.pl
chomp($str1=<STDIN>);
$res1=substr($str1,0,3);
print "FIRST FOUR CHARACTER OF THE STRING $res1\n";
$res2=uc(substr($str1,0,3));
print "FIRST FOUR CHARACTER OF THE STRING CONVERTED TO UPPERCASE $res2\n";
$res3=substr($str1,-3);
print "Last three character of the string $res3\n";
$res4=reverse(substr($str1,-3));
print "Last three character of the string in reverse order $res4\n";
$res5=substr($str1,4,-3);
print "Extract except first 4 and except last 3 and store in avariable $res5 \n";

OUTPUT
[ngdbprod@ngdb Vishwa_Perl]$ perl 8.pl
hellowworld
FIRST FOUR CHARACTER OF THE STRING hel
FIRST FOUR CHARACTER OF THE STRING CONVERTED TO UPPERCASE HEL
Last three character of the string rld
Last three character of the string in reverse order dlr
Extract except first 4 and except last 3 and store in avariable owwo
[ngdbprod@ngdb Vishwa_Perl]$

========================================================================
PROGRAM TO DEMO STRING MANUPULATION
========================================================================
chomp($str1=<STDIN>);
$n=index($str1,a);
substr($str1,$n,1,'1');
print "REPLACE THE FIRST OCCURANCE OF THE a with 1 $str1\n";
$n=rindex($str1,a);
substr($str1,$n,1,'2');
print "REPLACE THE LAST OCCURANCE OF THE a with 1 $str1\n";

OUTPUT
[ngdbprod@ngdb Vishwa_Perl]$ perl 9.pl
vishwanath
REPLACE THE FIRST OCCURANCE OF THE a with 1 vishw1nath
REPLACE THE LAST OCCURANCE OF THE a with 1 vishw1n2th
[ngdbprod@ngdb Vishwa_Perl]$

========================================================================&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&COMPARISON OPERATOR
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&


1. Numeric comparison --> '=='
2. String Comparison  --> 'eq'

Numeric Operators:
==                  --> eq
!=                   --> Not equal to
>                    --> Greater than
<                    --> Less than
>=                  --> Greater than or equal to
<=                  --> Smaller than or equal to


&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&Branching Constructs
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&


if statement
------------------------
if(expr)
{
statement;
}
else
{
statement;
}


if else if statement
------------------------
if(expr)
{
statement;
}
elseif(expr)
{
statement;
}
else
{
statement;
}

negated if statement
------------------------
if(!expr)
{
statement;
}
else
{
statement;
}

OR

unless(expr)
{
statement;
}
else
{
statement;
}

========================================================================PROGRAM TO DEMONSTRATE IF else
program accepts two numbers and checks whether the first digit of first
number and last digit of the second number are same
========================================================================


[ngdbprod@ngdb Vishwa_Perl]$ cat 11.pl
#! /usr/bin/perl
print "Enter a number\n";
chomp($str1=<STDIN>);
print "Enter another number\n";
chomp($str4=<STDIN>);
#$str2=substr($str1,0,1);
#$str3=substr($str4,-1);
#if($strr2 eq $str3)


if(substr($str1,0,1) eq substr($str4,-1))
{
print "First and last digits are same\n";
}
else
{
print "Digits are not same\n";
}
[ngdbprod@ngdb Vishwa_Perl]$
OUTPUT
Enter a number
12342
Enter another number
56437681
First and last digits are same

========================================================================PROGRAM TO CHECK WHETHER A STRING IS PALINDROME OR NOT Case insensitive
========================================================================


[ngdbprod@ngdb Vishwa_Perl]$ cat 10.pl
#! /usr/bin/perl
chomp($str1=<STDIN>);
$str1=uc($str1);
print "Upper case:$str1\n";
$str2=reverse($str1);
if($str1 eq $str2)
{
print "String is a palindrome\n";
}
OUTPUT
Nitin
Upper case:NITIN
String is a palindrome

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SWITCH STATEMENT
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

                IN C IN PERL
                 ----               -------
           switch(expr)                     given(expr)
             {         {
               case 1: when(1)
               case 2: when(2)
            }done         default
                }


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
LOOPS IN PERL
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*

**for
**while
**dowhile
**until
**do until
**foreach

=======================================================
PROGRAM TO DEMO WHILE LOOP
Accept a string and print it vertically
=======================================================
[ngdbprod@ngdb Vishwa_Perl]$ cat 12.pl
chomp($str1=<STDIN>);
#$i=1;
while($a=substr($str1,$i,1))
{
print "$a\n";
$i++;
}
[ngdbprod@ngdb Vishwa_Perl]$ perl 12.pl
vishwanath
v
i
s
h
w
a
n
a
t
h

==================================================================
FOREACH LOOP
==================================================================
$a=10;
$b=20;
$c=30;
foreach $i ($a,$b,$c)
{
$i--;
}
print "a = $a\n";
print "b = $b\n";
print "c = $c\n";

OUTPUT
[ngdbprod@ngdb Vishwa_Perl]$ perl 13.pl
a = 9
b = 19
c = 29

========================================================================
MISSING ALIAS In FOREACH LOOP
Here in this, instead of specifying "$i" after foreach, nothing specified.
Instead we get "$_" as default alias
========================================================================
$a=10;
$b=20;
$c=30;
foreach ($a,$b,$c)
{
$_--;
}
print "a = $a\n";
print "b = $b\n";
print "c = $c\n";

OUTPUT
[ngdbprod@ngdb Vishwa_Perl]$ perl 13.pl
a = 9
b = 19
c = 29

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@LISTS
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


A series of variables seperated by commas and enclosed in braces is LIST

**Assignment
----------
($a,$b)=(10,20);

**Swapping the values of variables
--------------------------------
($a,$b)=($b,$a);

**LIST indexing
----------------
($a,$b)=(10,20,30,40)[-1,2]
This will assign 40 and 30 to $a and $b respectively.
Quoted words(qw)
--------------------
('max','min')
OR
qw(max,min)

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ARRAYS
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

**How to declare a empty array
my @arr;

**Define a array with values
my @arr=(1,2,3,4,5);

**How to pick the first element
print "$a[0]"

**Print Last Element of array
print "$a[-1]"

**First 3 elements of array
print "@arr[0,1,2]"

**Print Complete array
print "@arr"

**Print the length of array
1. $n=$#arr+1
2. $n=scalar @arr
3. $n=@arr

========================================================================PROGRAM TO DEMO ARRAY OPERATIONS
========================================================================

print "Extract the first 5 elements of the array and store in array\n";
@arr=(10,20,30,40,50,60,70,80,90,100);
@a=@arr[0..5];
print "the first five elements are @a\n";

print " Increment the first five elements\n";
$i=0;
while($i<5)
{
$a[$i]=$a[$i]+1;
$i++;
}
OR
foreach (@a)
{
$_++;
}
print " Incremented the first five elements by one @a\n"

OUTPUT:
[ngdbprod@ngdb Vishwa_Perl]$ perl 14.pl
Extract the first 5 elements of the array and store in array
Contents of the array before manupulation 10 20 30 40 50 60 70 80 90 100
the first five elements are 10 20 30 40 50 60
Increment the first five elements
Incremented the first five elements by one 11 21 31 41 51 60

========================================================================
PROGRAM TO DEMO ARRAY OPERATIONS
Here, program accepts 5 user inputs into a empty array, and by
using inbuilt function of the PERL, prints the Largest,
smallest and sum of all the elements
========================================================================
[ngdbprod@ngdb Vishwa_Perl]$ cat 15.pl
use List::Util qw(max min sum);
print "Declare a empty array with name as nums\n";
my @nums;
foreach (0..4)
{
chomp($temp=<STDIN>);
push(@nums,$temp);
$_++;
}

print "Biggest element of the array\n", max(@nums);
print "\nSmallest element of array\n", min(@nums);
print "\nSum of all the elements\n", sum(@nums);

OUTPUT
[ngdbprod@ngdb Vishwa_Perl]$ perl 15.pl
Declare a empty array with name as nums
45
2
87
32
89
Biggest element of the array
89
Smallest element of array
2
Sum of all the elements
255

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
LIST OF INBUILD FUNCTIONS FOR ARRAYS
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


push(@arr, list)         --> Added at the end
pop(@arr) --> Deleted element at the end
unshift(@arr,list)         --> Added at the START
shift(@arr) --> Delete one element from the start
reverse(@arr)
sort(@arr)
grep
map
join
split


**sort(@arr
------------
sort by default sorts the string into ascending order and in ASCII order
syntax:
          @names= sort qw(................);


For sorting numbers
@nums = sort {$a <=> $b } @nums; where $a & $b are inbuilt variables

%%%%%%%%%%%%%%%%%%%%%%%%% perldoc -f sort %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


ex:
@name = qw (vish, naga, anil, mukta)

print join("\n",@names);
#sort the array
@names=sort{@a cmp @b} @names;

#Sort the array based on length of items of array
@names =  sort{ length($a) <=> length($b)} @names

==============================================================
How to print the elements of a array in new lines
==============================================================

@names = qw(vish naga anil mukta);

print join("\n",@names);
#sort the array
@names=sort{@a cmp @b} @names;
print "\n@names\n";
#Sort the array based on length of items of array
@names =  sort{ length($a) <=> length($b)} @names;
print "\n@names\n";

#sort them based on last char
@names = sort{ substr($a, -1) cmp substr($b, -1)} @names;
print "\n@names\n";

OUTPUT
[ngdbprod@ngdb Vishwa_Perl]$ perl 16.pl
vish
naga
anil
mukta
vish naga anil mukta

vish naga anil mukta

naga mukta vish anil
[ngdbprod@ngdb Vishwa_Perl]$

@@@@@@@@@@@@@@@@@@@@@@@@@@@
GREP
@@@@@@@@@@@@@@@@@@@@@@@@@@@
Syntax
@res= grep(/regex/,@arr);

Compares the regex accross each element of the array and returns the matching elements
===================================================================
#Display only those dates which fall in the current month
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% perldoc -f localtime %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
THe program uses perl function "localtime" which returns a list containing the details of current date.
The month is returned as a number where 0 denotes jan and 11 dec.
===================================================================

@arr=qw(15-4 10-2 11-4 17-4 23-5 24-6 30-7 12-3 29-3);
@localTime = localtime;
$mon=@localTime[4];
print "MONTH--> $mon\n";
@res = grep(/-$mon/,@arr);
print " Array elements in the current month @res\n";

OUTPUT
[ngdbprod@ngdb Vishwa_Perl]$ perl 17.pl
MONTH--> 3
Array elements in the current month 12-3 29-3
[ngdbprod@ngdb Vishwa_Perl]$

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
MAP
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IF any operatin needs to be performed on all the elements then use maps

Ex: Increment all the elements by 1
@arr=(10,20,30,50,60,90)
map($_++,@arr)

@names=qw(arun ram,rama,sita);
#store teh first char of the each elemet in @temp;
@temp=map(substr($_,0,1),@names);

Convert all the elemets of array to UPPER CASE
@names =map(uc,@names);

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SPLIT
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


Syntax
@res=split(/regex/,SCALAR);

To get the last word of the below sentence
$str='end of the program';
@res = split(/ /, $str)
$word = @res[3];

**If there are multiple delimeters in a sentence then we have to use sqare brackets
ex:
split(/[,:; \-]/, str);
** IF A deleimeter occurs more than once
ex:
if there are more than one space in a sentence
split(/ +/, @str);
**IF deleimeter is NULL, split splits the sentence character by character including space
$str='is of';
@res=split(//,$str);
$n=@res;
print "$n";

**IF deleimeter is / */, split splits the sentence character by character including space
$str='is of';
@res=split(//,$str);
$n=@res;
print "$n";

First Char of word
@res = split(/ /,@str)
@res = map($_, @res)
==================================================================================
#PROGRAM TO PRINT THE FIRST CHAR OF THE EVERY WORD IN SENTENCE ON A NEW LINE
==================================================================================
$str='end of the program';
@res = split(/ /, $str);
@res = map(substr($_,0,1), @res);
print join("\n", @res);

OUTPUT

[ngdbprod@ngdb Vishwa_Perl]$ perl 18.pl
e
o
t
p

@sales = qw(north-45 south-65 east-76 west-87)
@sum = split(/-/,@sales);
foreach(@sum){
$res+=$sum[i];
}

==================================================================================
#PROGRAM TO PRINT THE SUM of the numbers in the array (north-45 south-65 east-76 west-87);
==================================================================================


use List::Util qw(sum);

@sales = qw(north-45 south-65 east-76 west-87);
print "SALES-->@sales\n";

foreach (@sales){
@sum = split(/-/, $sales[$i]);
print "SUM-->@sum\n";
$i++;
$res=sum($res,$sum[1]);
}
print "$res\n";

OUTPUT
[ngdbprod@ngdb Vishwa_Perl]$ perl 19.pl
SALES-->north-45 south-65 east-76 west-87
SUM-->north 45
SUM-->south 65
SUM-->east 76
SUM-->west 87
273

===================================================================================
PROGRAM TO PARSE THE TEXT FILE, the program takes the file name as argument
===================================================================================

INPUTFILE:
 cat data.log
10001 name1 place1 15000,2000,500
10002 name2 palce2 12000,1000,4
10003 name3 palce3 12345,0000,1
10004 name4 palce4 12312,-1,0011


Program:
use List::Util qw(sum);

@arr = <>;
foreach (@arr) {
@fileSplit=split(/ /,$_);
print "After first split\n \t\t@fileSplit\n";
foreach (@fileSplit){
@salary=split(/,/,$fileSplit[3]);
$totalSalary = sum(@salary);
#$totalSalary = sum(salary[1] salary[2] salary[3]);
}
print "Total Salary -->\n \t\t$totalSalary\n";
}

OUTPUT
After first split
10001 name1 place1 15000,2000,500

Total Salary -->
17500
After first split
10002 name2 palce2 12000,1000,4

Total Salary -->
13004
After first split
10003 name3 palce3 12345,0000,1

Total Salary -->
12346
After first split
10004 name4 palce4 12312,-1,0011

Total Salary -->
12322
[ngdbprod@ngdb Vishwa_Perl]$ cat 20.pl

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Traversing the array
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
method1:


**SPecial OPERATORS IN PERL
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% perldoc -f operators
IN Perl 5.10 above, special operators inducted for traversing
================================================================
TO COMPARE TWO ARRAYS
================================================================
@a=(10,20,30);
@b=(50,60,70);
if(@a~~@b)
{
print "EQUAL";
}
else
{
print "NOT EQUAL";
}

FOR SPECIAL MATHEMATICAL OPERATORS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% perldoc  Algorithm::Diff %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

LCS = Intersection
DIFF= Minus


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
HASHES
Its a two column table, where first
column contains the key ans second column contains the value
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

scalar
  |
 LIST
  /\
 /  \
/    \
 Array   Hash-Unordered List
Ordered List

** DECLARE A EMPTY HASH
my %hash
** DECLARE A HASH WITH ITEMS
%hash = (a,b,c,d,r,f,g,h);

**IN HASH, all the elements are stored as key value pair, one key can have only one value

**How to get the value of a key in HASH
$value = $hash{key};
** SPECIFIYING MULTIPLE KEYS
@list = @hash{key1,key2,key3};
** HOW TO GET COMPLETE HASH
print "%hash";



Inbuilt Functions in HASH
-delete
$hash{arun}--> This will delete the key value pair of 'arun'
-exists
$hash{hari}--> Returns 1 if that key exists in HASH
Returns undef if not found
-keys
-values
-each
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% perldoc -q duplicate %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


** how to traverse the hash
method 1:
while(($a, $b)=each(%has))
{
print "$a $b\n";
}
method 2:
foreach $i(keys(%hash))
{
print $i $hash{$i}\n;
}

**SORT HASH based on keys
@temp=sort keys(%hash)
** SORT HASH BASED ON VALUES
@temp=sort{$hash{$b}<=>$hash{$a}} keys(%hash)

INPUT:
$str='two three five one nine';

Output
23519
SOLUTION:
%hash=qw(zero 0 one 1 two 2 three 3 four 4 five 5 six 6 seven 7 eight 8 nine 9);

print @hash{split(/ /,$str};


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
COMPLEX DATA STRUCTURES
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

How to merge the multiple arrays into one?
**@arun=(10,20,30);
@hari=(23,43,65);
@chet=(54,65,76,90);

@merged_Array=(@arun,@hari,@chet);

**HOW To STORE THE REFERENCES OF ARRAYS:

@merged_Array=(\@arun,\@hari,\@chet);

**TO ACCESS THE INDIVIDAUL ELEMENTS:
$arr[0]->[0] OR $arr[0][0] --> This will give first element of array arun i.e.10

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Anonymous Array
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@arr = (10,20,30,40)---> THis is named array
$ref=[10,20,30,40]; ---> This is anonymous array
print "$ref->[0]";--------> THis will print first element of array
print "$ref"; -------------> This will print address array
print @$ref    -------->This will print complete array

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Anonymous hash
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


@arr = {arun=>10,shyam=>20,mina=>30,tina=>40}
$arr = {arun=>10,shyam=>20,mina=>30,tina=>40}  Anonymous Hash
print "$ref"       Address of hash
print "$ref->{arun}"; value of Arun
print %$ref; WHole hash is printed



$testCase1={functionname=fn1,actualResult=undef,expectedResult=10}
$testCase2={functionname=fn2,actualResult=undef,expectedResult=10}
$testCase3={functionname=fn3,actualResult=undef,expectedResult=10}
$testCase4={functionname=fn4,actualResult=undef,expectedResult=10}
$testCase5={functionname=fn5,actualResult=undef,expectedResult=10}

Call the testcases by below line
$red=[$tc1,$tc2...........]

foreach $i (@$ref)
{
$res=&$i->{name}
}










@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DEBUGGING THE DATASTRUCTURES WITH "DUMPER"
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

sytax
use Data::Dumper

$ref=..................

print Dumper($ref);


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
REGULAR EXPRESSION
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SPECIAL METACHARS

%%%%%%%%%%%%%%%%%%%%% perldoc perlcheat %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
REGEX METACHARS            REGEX MODIFIERS
          ^     string begin         /i case insens.
          $     str. end (before \n) /m line based ^$
          +     one or more          /s . includes \n
          *     zero or more         /x ign. wh.space
          ?     zero or one          /g global
          {3,7} repeat in range
          ()    capture          REGEX CHARCLASSES
          (?:)  no capture       .  == [^\n]
          []    character class  \s == [\x20\f\t\r\n]
          â     alternation      \w == [A-Za-z0-9_]
          \b    word boundary    \d == [0-9]
          \z    string end       \S, \W and \D negate


$str='varun';
if($str=~/arun/)--------> Does the str contain arun ------> YES TRUE
if($str=~/^a/)--------------------> DOES THE STRING BEGIN WITH a? ------> NO FALSE
if($str=~/arun$/)------------> DOES THE STRING END WITH arun?----------->YES TRUE

[0-9] OR \d
[a-zA-Z0-9] OR \w
[ \t\n] OR \s

[!0-9] OR \D
[!a-zA-Z0-9] OR \W
[! \t\n] OR \s

DAY3

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Subroutines
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

**Subroutines are written before they are called
** Arguments passed to the subroutine are copied into a special array named as "@_"
Syntax:
sub add
{
$res=$_[0] + $_[1];
return $res;
}

add(arg1, arg2)


Whenever a array or hash been passed as a argument, then the reference of the array is to be passed as an argument.
Ex:
@a=(5,6,7);
@b=(1,2,3);
sub func_Call
{
my ($p, $q)=@_;
print "$p->[0]";-----------> Will print the first element of the first array
print "$q->[0]";-----------> Will print the first element of the second
array
}
func_Call(\@a, \@b)


**Lexically scoped variables

Variables declared witht he keyword "my" within subroutine, such variables are termed lexical variable.
"our" variables are global variables.

**NOTE Don't declare any "our" variable withing subroutine.
** Is it compu;lsory to declare the variables in PERL?
Ans: NO,
But when program has

use Strict;
use Warnings;

Then it is mandatory to declare the variables. We have to specify whether that is local variable of global variable.

Package varibles:
The variales which are defined outside a module are called package variables. There scope is accross the package.

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
PROGRAM TO DEMO PACKAGES
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
**Below is a package, it is saved as .pm file

[ngdbprod@ngdb Vishwa_Perl]$ cat mylib.pm
package mylib;

sub func1
{
        print "Hello\n";
}
1;
It should always end with 1, so it returns true always

** In the program below, I am including the package mylib by keyword "use".
And used scope resolution operator "::"

[ngdbprod@ngdb Vishwa_Perl]$ cat 24.pl
use mylib;

mylib::func1();
[ngdbprod@ngdb Vishwa_Perl]$


**Every package in PERL has two functions by default. These are called Package constructor and Package destructor.
sub BEGIN,
{

}
sub END
{

}

TO ADD our libraries to the Compilation,
include the below line
 use lib ("path to the library");

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% perldoc -q @INC %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


Exporting and Importing
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% perldoc Exporter %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


NAME
       Exporter - Implements default import method for modules

SYNOPSIS
       In module YourModule.pm:

         package YourModule;
         require Exporter;
         @ISA = qw(Exporter);
         @EXPORT_OK = qw(munge frobnicate);  # symbols to export on request

       or

         package YourModule;
         use Exporter âimportâ; # gives you Exporterâs import() method directly
         @EXPORT_OK = qw(munge frobnicate);  # symbols to export on request

       In other files which wish to use YourModule:

         use ModuleName qw(frobnicate);      # import listed symbols
         frobnicate ($left, $right)          # calls YourModule::frobnicate

DESCRIPTION
       The Exporter module implements an "import" method which allows a module to export functions and variables to its usersâ namespaces. Many modules
       use Exporter rather than implementing their own "import" method because Exporter provides a highly flexible interface, with an implementation opti-
       mised for the common case.

       Perl automatically calls the "import" method when processing a "use" statement for a module. Modules and "use" are documented in perlfunc and
       perlmod. Understanding the concept of modules and how the "use" statement operates is important to understanding the Exporter.

       How to Export

       The arrays @EXPORT and @EXPORT_OK in a module hold lists of symbols that are going to be exported into the users name space by default, or which
       they can request to be exported, respectively.  The symbols can represent functions, scalars, arrays, hashes, or typeglobs.  The symbols must be
       given by full name with the exception that the ampersand in front of a function is optional, e.g.

           @EXPORT    = qw(afunc $scalar @array);   # afunc is a function
           @EXPORT_OK = qw(&bfunc %hash *typeglob); # explicit prefix on &bfunc

       If you are only exporting function names it is recommended to omit the ampersand, as the implementation is faster this way.

       Selecting What To Export

       Do not export method names!

       Do not export anything else by default without a good reason!

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
How to install a module
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

  ** Needs to be searched for the module in "cpan.org"
Remember the path.
Then execute on the local prompt
1: cpan --> THis will return the CPAN prompt
2: install tree::Binary
3: quit
4: perldoc tree::Binary--> If installed successfully, it should show the documentation

You should be a admin or a sudo user


** How to list all the modules?
>> instmodsh
** How will I know whether the module is installed or not
>> perldoc moduleName


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% perldoc SelfLoader %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% perldoc AutoLoader %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
OBJECT ORIENTED PERL
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% perldoc -f bless %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% perldoc UNIVERSAL %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% perldoc DBI %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

##################################################################################
PROGRAM TO DEMO DATABASE ACTIVITIES IN PERL
#####################################################################################
use DBI;
$dbh = DBI->connect('dbi:odbc:mydriver');
$stmt = $dbh->prepare("select * from emps");
$stmt->execute();

while (@arr=$stmt->fetchrow_array())
{
print "@arr";
}
$dbh->disconnect();

**HOW to execute a shell command within a PERL Program

method1:
system("command");

method2:
@result=`command`;
OR
@result=qx(command); -----------> qx -->Quoted execution

method3:---------> Object oriented way of using method1 and method2
use ARGV;
$r1 = Argv->new;
$r1->exec;



#####################################################################################
IPC
#####################################################################################
FH--> File Handler

open(FH,"dir |");
print <FH>;

close FH;

OUTPUT: LISTS the content of the directory
[ngdbprod@ngdb ~]$ perl 25.pl
25.pl
ambari-0.9
ambari-1.2.0-incubating
ambari-1.2.1-incubating
ambari-1.2.3-incubating
ambari-1.2.3-incubating.tar.gz
Build
fuct.sh
hive-0.11.0-shark-SNAPSHOT-bin.tar.gz
hive-0.11.0-shark-SNAPSHOT-bin.tar.gz.md5
NGDB_RPMS
NGDB_SVN_CODE
NSN-NGDB-POSTGRES-0.1.0-9.1.4.noarch.rpm
shark-0.8.0-bin.tar.gz
sonar-3.3.1
sonar-3.3.1.zip
spark-0.8.0-incubating-bin.tar.gz
Vishwa_Perl
[ngdbprod@ngdb ~]$

Tuesday, April 22, 2014

Shell Scripting

========================================================================
sequence command
========================================================================
seq 1 10
seq 1 2 10 --> Generates odd numbers
seq 0 2 10 --> Generates even numbers
[root@ngdb ~]# seq 0 2 10
0
2
4
6
8
10
========================================================================
wc
========================================================================
--> wc -c--> character count
--> wc -l--> line count
--> wc -w--> word count

cat <filename> | wc -w --> Gives words in a file.
========================================================================
Head and Tail command
========================================================================
How to show only part of file, i.e from line 21 25
[root@ngdb ~]# cat nums.txt | head -25 | tail -5
21
22
23
24
25
[root@ngdb ~]#

to see first char of a word
========================================================================
[root@ngdb ~]# echo "hello" | head -c1
h[root@ngdb ~]#
[root@ngdb ~]#
[root@ngdb ~]# echo "hello" | head -c3
hel[root@ngdb ~]#
[root@ngdb ~]# echo "hello" | head -c3 | tail -c2
el[root@ngdb ~]#
[root@ngdb ~]#
[root@ngdb ~]#
[root@ngdb ~]#
[root@ngdb ~]# echo "hello" | head -c3 | tail -c3
hel[root@ngdb ~]#
[root@ngdb ~]#
[root@ngdb ~]#
========================================================================
Cut command
========================================================================

[root@ngdb ~]# echo "hello" | cut -c2
e
[root@ngdb ~]# echo "hello" | cut -c1-4
hell
[root@ngdb ~]#
[root@ngdb ~]#
 echo "hello" | cut -c1,4
hl
[root@ngdb ~]#
[root@ngdb ~]#
[root@ngdb ~]#
[root@ngdb ~]# echo "main:kill::fare:::" | cut -d":" -f1
main
[root@ngdb ~]#
[root@ngdb ~]#
[root@ngdb ~]# echo "main:kill::fare:::" | cut -d":" -f2
kill
[root@ngdb ~]# echo "main:kill::fare:::" | cut -d":" -f3

[root@ngdb ~]# echo "main:kill::fare:::" | cut -d":" -f4
fare
[root@ngdb ~]#

========================================================================
trim command
========================================================================
[root@ngdb ~]# ls  -l
total 3910828
-rwxrwxrwx 1 root root        745 Sep 25  2012 !
-rwxrwxrwx 1 root root       1459 Sep 10  2012 anaconda-ks.cfg
drwxrwxrwx 2 root root       4096 Sep 10  2012 Desktop
-rwxrwxrwx 1 root root      12232 Apr  9  2013 epel-release-5-4.noarch.rpm
-rwxrwxrwx 1 root root      56384 Sep 10  2012 install.log
-rwxrwxrwx 1 root root       4069 Sep 10  2012 install.log.syslog
-rwxrwxrwx 1 root root 3997462528 Apr  9  2013 NGDB-OS-5U7_64bit_V3.iso
-rw-r--r-- 1 root root        292 Mar 10 12:31 nums.txt
drwxr-xr-x 2 root root       4096 Dec  9 16:24 old_sysfs_files
-rwxrwxrwx 1 root root    1227721 Apr  9  2013 php53-mbstring-5.3.3-1.el5_6.1.x86_64.rpm
-rw-r--r-- 1 root root     130686 Apr 12  2013 php-apc-3.1.9-3-mdv2011.0.x86_64.rpm
-rw-r--r-- 1 root root      40160 Apr 12  2013 php-apc-4.3.10_2.0.4-4mdk.x86_64.rpm
-rwxrwxrwx 1 root root     465536 Apr  9  2013 php-mbstring-5.3.3-22.el6.x86_64.rpm
-rwxrwxrwx 1 root root    1307354 Apr  9  2013 php-mbstring-5.5.0-0.20.201303201430.el5.remi.x86_64.rpm
-rwxrwxrwx 1 root root       4148 Feb 12  2013 settings.xml
[root@ngdb ~]#
[root@ngdb ~]# ls  -l| tr -s " "
total 3910828
-rwxrwxrwx 1 root root 745 Sep 25 2012 !
-rwxrwxrwx 1 root root 1459 Sep 10 2012 anaconda-ks.cfg
drwxrwxrwx 2 root root 4096 Sep 10 2012 Desktop
-rwxrwxrwx 1 root root 12232 Apr 9 2013 epel-release-5-4.noarch.rpm
-rwxrwxrwx 1 root root 56384 Sep 10 2012 install.log
-rwxrwxrwx 1 root root 4069 Sep 10 2012 install.log.syslog
-rwxrwxrwx 1 root root 3997462528 Apr 9 2013 NGDB-OS-5U7_64bit_V3.iso
-rw-r--r-- 1 root root 292 Mar 10 12:31 nums.txt
drwxr-xr-x 2 root root 4096 Dec 9 16:24 old_sysfs_files
-rwxrwxrwx 1 root root 1227721 Apr 9 2013 php53-mbstring-5.3.3-1.el5_6.1.x86_64.rpm
-rw-r--r-- 1 root root 130686 Apr 12 2013 php-apc-3.1.9-3-mdv2011.0.x86_64.rpm
-rw-r--r-- 1 root root 40160 Apr 12 2013 php-apc-4.3.10_2.0.4-4mdk.x86_64.rpm
-rwxrwxrwx 1 root root 465536 Apr 9 2013 php-mbstring-5.3.3-22.el6.x86_64.rpm
-rwxrwxrwx 1 root root 1307354 Apr 9 2013 php-mbstring-5.5.0-0.20.201303201430.el5.remi.x86_64.rpm
-rwxrwxrwx 1 root root 4148 Feb 12 2013 settings.xml
[root@ngdb ~]#

[root@ngdb ~]#
[root@ngdb ~]# echo -e "a b\t\tc d" | tr  "\t" " " |tr -s " "| cut -d" " -f3
c
[root@ngdb ~]#
[root@ngdb ~]#
========================================================================
TO COUNT THE WORDS WITH SUBSTITUTION
========================================================================
[root@ngdb ~]# echo "helllo-world-comms"| wc -w
1
[root@ngdb ~]#
[root@ngdb ~]#

[root@ngdb ~]# echo "helllo-world-comms"|tr "-" " "| wc -w
3
[root@ngdb ~]#
[root@ngdb ~]#
TO CHANGE THE CASE
echo "hello" | tr "[a-z]" "[A-Z]"
[root@ngdb ~]# echo "hello" | tr "[a-z]" "[A-Z]"
HELLO
[root@ngdb ~]#
TO COUNT THE VOWELS
-d--> delete
-c--> compliment
echo " hellow world of unix" | tr -dc "[aeiou]" | wc -c
[root@ngdb ~]# echo -n " hellow world of unix" | tr -dc "[aeiou]" | wc -c
6
[root@ngdb ~]#
========================================================================
paste command
========================================================================
[root@ngdb ~]# seq 1 6 > 1.txt
[root@ngdb ~]# seq 7 10 > 2.txt
[root@ngdb ~]#
[root@ngdb ~]#
[root@ngdb ~]# cat 1.txt 2.txt
1
2
3
4
5
6
7
8
9
10
[root@ngdb ~]# paste 1.txt 2.txt
1       7
2       8
3       9
4       10
5
6
[root@ngdb ~]#
[root@ngdb ~]# paste -d" " 2.txt 1.txt
7 1
8 2
9 3
10 4
11 5
12 6
13
14
15
16
17
18
19
20
[root@ngdb ~]# paste -s 2.txt 1.txt
7       8       9       10      11      12      13      14      15      16     17       18      19      20
1       2       3       4       5       6
[root@ngdb ~]#
========================================================================
SORT COMMAND
========================================================================
SORT the file based on first column

[root@ngdb ~]# cat > emp.txt
guru pmp blt 789
tan  cem blr 098
mike OS mun  890890
Reeema Fin Mum 87123
Lagoo Fin Mum 1241



[root@ngdb ~]#
[root@ngdb ~]#
[root@ngdb ~]# vi emp.txt
[root@ngdb ~]# sort emp.txt


guru pmp blt 789
Lagoo Fin Mum 1241
mike OS mun  890890
Reeema Fin Mum 87123
tan  cem blr 098
[root@ngdb ~]#
*********************************************************************************
SORT the  file based on second column
*********************************************************************************
[root@ngdb ~]# sort -k2 emp.txt


tan  cem blr 098
Lagoo Fin Mum 1241
Reeema Fin Mum 87123
mike OS mun  890890
guru pmp blt 789
[root@ngdb ~]#

sort -r emp.txt  --> Reverse order
sort -n -k4 emp.txt. --> numeric sort
sort -t ":" -k2 emp.txt --> with delimeter
sort -f emp.txt --> ignore case

sort emp.txt -o emp.txt --> To store the sorted details
*********************************************************************************
Command to find the employee who is drawing highest salary in emp.txt
*********************************************************************************
[root@ngdb ~]#
[root@ngdb ~]# sort -n -k4 emp.txt | tail -1 | cut -d" " -f1,4
mike
[root@ngdb ~]#

[root@ngdb ~]# cat emp.txt
guru pmp blt 789
tan  cem blr 098
mike OS mun  890890
Reeema Fin Mum 87123
Lagoo Fin Mum 1241



[root@ngdb ~]#
[root@ngdb ~]# sort -nr -k4 emp.txt | head -1 |tr -s " " | cut -d " " -f1,4
mike 890890
[root@ngdb ~]#
*********************************************************************************
For Unique values
*********************************************************************************
[root@ngdb ~]# sort -u emp.txt | tr -s " " | cut -d " " -f2

pmp
Fin
OS
Fin
cem
[root@ngdb ~]#
*********************************************************************************
COUNT THE NUMBER OF FILES IN THE DIRECTORY WITH FILE TYPES
*********************************************************************************
[root@ngdb ~]# ls  *.* | cut -d"." -f2 | sort | uniq -c
      1 1
      3 3
      1 5
      1 cfg
      1 iso
      2 log
      1 noarch
      4 txt
      1 xml
[root@ngdb ~]#
*********************************************************************************
TO LIST NUMBER OF FILES AND DIRECTORIES IN A FILE
*********************************************************************************
[root@ngdb ~]# ls -la   | cut  -c1 |sort | uniq -c
     28 -
     20 d
      1 t
[root@ngdb ~]#
[root@ngdb ~]#
*********************************************************************************
TO CHECK HOW MANY USERS HAVE LOGGED IN THE SYSTEM
*********************************************************************************
[root@ngdb ~]#
[root@ngdb ~]# who |cut -d" " -f1| sort | uniq -c
      2 ngdbprod
      5 saiUser
[root@ngdb ~]#
uniq -c --> Count
uniq -d --> display only duplicates
uniq -u --> display only uniques
========================================================================
cmp command Compare two files byte by byte and sstops when it encounters a difference
========================================================================

comm COMMAND--> Compare two sorted columns
=======================================================================================
comm file1 file2
[root@ngdb ~]# comm 1.txt 2.txt
1
        1
                2
                3
                4
5
6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
[root@ngdb ~]#
Column 1--> Colmn1-Colmn2
Column 2--> Colmn2-Colmn1
Column 3--> Colmn1 Intersection Colmn2
==================================================================================
tee command
==================================================================================
==================================================================================
grep command
==================================================================================
grep --> Supports only Basic REGEX expressions
egrep --> Support both Basic REgular expression and EXTENDED reg expression
fgrep --> Doesn't support REGEX

Basic REGEX                                 EXTENDED REGEX
    .   -->A char ? --> zero/one
[ ] --> Group +      --> one or more
^ --> Line starts with {m,n}  --> repeatative
$ --> Line ends with |    -->Alteration
\<  -->Words start with
\> -->Words ends with ( )  -->Overrode precedence
*   -->zero/more
*********************************************************************************
Select all blank lines in file
*********************************************************************************
--> "^ *$"
*********************************************************************************
Find the folders in folder
*********************************************************************************
--> ls -l | grep "^d"
[root@ngdb ~]#  ls -l | grep "^d"
drwxrwxrwx 2 root root       4096 Sep 10  2012 Desktop
drwxr-xr-x 2 root root       4096 Dec  9 16:24 old_sysfs_files
[root@ngdb ~]#

grep OPTIONS
 -i ignore case
 -c count no of matching lines
 -n display the matching numbers
 -v display the lines that doesn''t match
 -l display matching file names

 ----------------------------------------
 diplay emps working for sales and accounts
 egrep "(sales|accounts)" emp.txt
 --------------------------------------------
 ========================================================================
 find COMMAND --search a file based on its attributes

 syntax: find path -attributes -action
 attributes:
name
permission
mtime
size
type
actions:
print
ls
exec  (non-interactive execution)
ok   (interactive execution)
*********************************************************************************
==>Delete all the .tmp files in my login without my interaction
*********************************************************************************
[root@ngdb ~]#find ~ -type f -name "*.tmp" -exec rm {} ;
rm `find ~ -type f -name "*.tmp"`
*********************************************************************************
==> Command to move all the files whose size is greater than 1 GB to /tmp/BIG
*********************************************************************************
[root@ngdb ~]#find / -size +1G -type f -exec mv {}/tmp/BIG
=====================================================================================
&&  and ||
(CONDITIONAL COMMAND GROUPING)
*********************************************************************************


DELETE THE FILE IF IT EXISTS
*********************************************************************************
ls tmp.txt && rm tmp.txt



*********************************************************************************
CHECK THE FILE, CREATE IT IF DOESN'T EXITS
*********************************************************************************

ls tmp.txt && touch tmp.txt

****************************** LOOPS IN SHELL
******************************** COMPARE
|
       /\
while loop               /  \       String  Number
for loop     $a = $b   $a -eq $b     $a!= $b -ne
nested for loop -ge                                 -gt
                                                                                -le
-lt WHILE LOOP
SYNTAX:
while test CONDITION
do
statements
done
OR
while [condition] UNTIL LOOP do                         until [condition]
  statement do done                       statement
done
FOR LOOP
SYNTAX:

for i in 10 20 30 40
do
statement
done


IF ELSE
if test CONDITION
then
statement
else
statement
fi
OR
if [ CONDITION ]
then
statement
else
statement
fi
---------------------------------------------
Command substitution



DAY2
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

FUNCTIONS
========================================================================All the variables in shell script by default are GLOBAL

Local variables can be declared with keyword "local"

/* TO DEMO LOCAL AND GLOBAL VARIABLES*/
testing()
{
local str="Hello I M in this world"
echo "$str"
}
str="Helooi Who are you"
echo $str
testing
echo $str


OUTPUT
[root@ngdb VISHWA]# sh  file4.sh
Helooi Who are you
Hello I M in this world
Helooi Who are you
[root@ngdb VISHWA]#

========================================================================
ways of executing the script
========================================================================
1> sh script.sh
2> ./script.sh (make the script executable by adding permissions)
3> . script.sh (source script.sh)

========================================================================
Special variables of shell script
========================================================================
1> Command line arguments

$$ --> Gives the pid of the current process
$! --> Gives the pid of the last executed background process (previously completed process)
$? --> Gives the exit status of the last executed command( 0 for success, non-zer for failure)
$1-$9 --> Command line arguments
$# --> Total count of arguments
$*/$@ --> List of arguments


#PROGRAM TO DEMO COMMAND LINE ARGUMENTS */
#PROGRAM SHOULD DO */
#TESTCASE1***/
#SHOULD COMAPRE BOTHE THE FILES< PRINT WHETHER SAME OR DIFFERENT, CHECK WHETHER BOTH THE FILE EXISTS ALSO CHECK IF SUFFICIENT ARGUMENTS ARE PASSED */



if [ $# -lt 1 -o $# -gt 2  ]
then
        echo "Enter the valid number of the arguments"
        exit;
else
        if [ -e $1 -a -e $2 ]
                then
                        echo "Comparing the two files"
                        cmp $1 $2 > /dev/null
                        if [ $? -eq 0 ]
                                then
                                        echo "Two files are same"
                                else
                                        echo "Two files are different"
                        fi
                else
                        echo "Error: One of the files doesn't exists"
        fi
fi

shift is used to traverse through the command line arguments
========================================================================
SIGNAL HANDLING
========================================================================
#PROGRAM TO HANDLE THE SIGNAL like ctrl+c ctrl+d
# trap is used to handle any signal
#syntax is " trap 'action' <signal>
#NOTE:
$ SIGSTOP, SIGKILL SIGGSTP can not be trapped and can not be handled by trap command as they are non-maskable interrupts

trap ' echo "Invalid signal:sleep 2"' SIGINT
for i in `seq 1 10`
do
        echo $i
        sleep 2
done
================================================================================
getopts command
================================================================================
================================================================================
sed command
================================================================================
  stream editor
  u can execute major editor commands
 
  how to print first 5 lines of file
  head -5 one.txt | tail -1
  or
  sed -n '5p' one.txt
 
  how to print 21 to 25 lines of a file
  sed -n '21,25p' one.txt
  sed -n '20,$p' one.txt
 
  how to print last five lines
 
  how to delete blank lines of files
  sed '^$/d' one.txt
 
  How to search and replace
  sed '/s/hello/bye/gi' one.txt
 
ADD TEXT
----------------
sed 'a\what to be add\'

CHANGE THE LINE
----------------
sed 'c\what to be changed\'

INSERT THE TEXT
----------------
sed 'i\what to be inserted\'
===============================================================================
awk
===============================================================================
-->Advanced filter
--> Useful for tabular data
--> Reporting Language
--> Pseudo "C" compiler
--> No need of trimming
--> Can check in specific column search
syntax:

awk -options '{cmds}' filename

How to print first two columns of a file
awk -F" " '{print $1 $2}' <filename.txt>

to print first n last line of ls -l command
ls -l | awk '{print $1 $9}'

How to check in perticular column
awk '{}$2~/sales/{print}' emp.txt
OR


BEGIN{----------}
END{---}
awk '{sum=sum+$4}END{print sum}' emp.txt


awk Special Variables

NR-->Number of ROWS
NF--> NUMBER of Fields
RS--> ROW seperator
FS--> FIeld Seperator
ORS-->o/p row seperator
OFS--> o/p field seperator

awk 'NR==5{print}' num.txt ---->Prints the fifth line
awk 'NR>=1 && NR<=5{print}' num.txt ------------->Prints the first five lines
awk '{print $NF}' num.txt -------> Prints the last column of hte file

=====================================================================
awk scripts
=====================================================================
Syntax:

BEGIN {
BEGINBLOCK
}
{
BLOCK
}
END{
ENDBLOCK
}

My file has 20 lines
how many time BEGINBLOCK gets executed = 1
how many time ENDBLOCK gets executed = 1
how many time BLOCK gets executed = 20