Wednesday, 18 January 2012

Unix Perl - Some useful commands.

Here's some useful quick tips in Unix Perl.

1) To see Perl DBI installed and its version  :
perl -e 'use DBI; print $DBI::VERSION,"\n";'
2) To see Perl DBD installed and its version

perl -e 'use DBD::Oracle; print $DBD::Oracle::VERSION,"\n";'

3) Just to Connect to Oracle :

use strict;
use DBI;
my $dbh = DBI->connect('dbi:Oracle:fs1pa_fs1p','<uname>','<passwd>',)|| die "DB conn not made: $DBI::errstr";
$dbh->disconnect;

4) Connect to Oracle and simple Query
use strict;
use DBI;
my $dbh = DBI->connect('dbi:Oracle:fs1pa_fs1p','<uname>','<passwd>',)|| die "DB conn not made: $DBI::errstr";
my $sql = qq{ SELECT TNAME,TABTYPE FROM TAB };
my $sth = $dbh->prepare($sql);
$sth->execute();
my($tname,$tabtype);
$sth->bind_columns(undef, \$tname, \$tabtype);
print "List of Table:\n\n";
while ($sth->fetch() ) {
        print "Object :$tname, Type :$tabtype\n";
}
$sth->finish();
$dbh->disconnect;


5) insert line numbers in a file 
perl -pi -e'$_ = sprintf "%04d %s", $. $_' filename

6) change pattern 'print' to 'printf' and change in same file
perl -e 's/print/printf/g' -p -i  file_name

7) Print no.of lines
perl -le 'open FILE, "file.txt";@_=<FILE>; print $.'

8) just lines 4 to 6
perl -ne 'print if 4 .. 6' file.txt
perl -ne 'print if 4..6' file.txt

 9) Print first column values from line 4 to 6
 perl -F'\|' -lane 'print $F[0] if 4..6' file.txt

10) Avoids the blank lines and print only the lines with contents
perl -ne 'print unless /^$/../^$/' test.txt

 11) Prints only the matching words
perl -lne '/Mukundan/ and print $&' file.txt


12) Prints the entire line
perl -lne '/Mukundan/ and print $_' file.txt
 

13) Get all the PACKAGE objects from oracle
use strict;
use DBI;
my $dbh = DBI->connect('dbi:Oracle:fs1pa_fs1p','<user>','<passwd>',)|| die "DB conn not made: $DBI::errstr";
my $sql = qq{ SELECT OBJECT_NAME FROM USER_OBJECTS WHERE OBJECT_TYPE=? };
my $sth = $dbh->prepare($sql);
$sth->execute("PACKAGE");
my($oname);
$sth->bind_columns(undef, \$oname);
print "List of Packages:\n\n";
 while ($sth->fetch() ) {
        print "Packages :$oname\n";
}
 $sth->finish();
$dbh->disconnect;

1 comment:

  1. Now your every network related issues, network fluctuations, bad signal reception leading to call drops or slow internet speed moments from now on will not be something you have to suffer more as we have invented and brought for you Universal Antenna (for Mobile, Dongle and Router).


    BUY NOW :- Mobile Antenna India

    ReplyDelete