Skip to content

Print few lines from a file

Here is the situation – you have a huge file and you wanted to get few lines from the file.
perl -ne 'print if $. >= 3300 && $. <= 5000' myhugefile.txt
This perl oneliner gets the lines from 3300 to 5000 and print to standard output.

Its also easy in perl to print only a specific line, here it goes
perl -ne'print, last if $. == 10000' filename.txt
This onliner will get you the 10000th line printed !!

count of match / count of regular expression matched

Ever wondered how to find the count of a special character in a file ?
For example you wanted to get the total number of “|” in a file , you can do it easily using a perl onliner
perl -ne '$n+=tr/|//;END{ print "$n\n"}' input.txt
Thats it, don’t you love perl for quick and dirty tricks ??

Perl Oneliner

Hi All,
I found a very good perl ebook , Perl One-Liners Explained written by Peteris Krumins. This is very handy if you work a lot with text processing, log file monitoring and for certain admin purpose.
I follow his blog, it contains awesome tips for unix lovers. Lots of onliners in perl, awk and sed which can really help to improve your productivity. Most of the one liners are self explanatory and author has also provided detailed description this makes the reading fun and enjoyable.
So happy reading friends. !!

Extract a procedure from DB2 database

How to get a copy of compiled procedure in a DB2 database ?

There are more than a way to do this, I usually use export functionality of DB2. Now lets see how to do it with a perl script.

#!/usr/bin/perl -w

#############################################################################################################
# NAME........... extr_proc.pl
# AUTHOR......... Govind Kailas (govindkailas@gmail.com)
# DATE........... Tue Jul 19 18:42:54 2011
# PURPOSE........ Extract procedure from database
# ARGUMENT....... procedure name
#############################################################################################################
$proc_nm=shift;
die "Pls provide the procedure name" if $proc_nm eq "";
$proc_nm=~s|.*\.||gi; #if user provided SCHEMANAME.PROC_NAME as argument , remove the SCHEMANAME. we need only proc_nm

#=============================================================================================
#Connecting to the database
#=============================================================================================

my ($DB_NAME,$USER,$PWD)=(database_name,user_id,password); #define the variables with appropriate values
$db2connect=`db2 connect to $DB_NAME user $USER using $PWD`; #connect to the database
print "db2 connect value : $db2connect";

`db2 "export to $proc_nm.spl of del select text from syscat.procedures where procname='$proc_nm'"`;
print "Procedure extracted to $proc_nm.spl\n";

Now you can extract any procedure from the DB2 database using this script

eg :

perl extr_proc.pl MYSCHEMA.MYPROC 

If the procedure exists in the database you will see a new file named MYPROC.spl (as per the above example)
and if not you will see a zero sized file.

Wish you all a very happy new year 2012 !!

Happy New year to all - A poster from one of my all time fav movie UP

Script to Monitoring Tablespace in DB2

I often come across situation when developers yelling at me saying their script fails because of filled table space.
They have some scripts to pull huge data files from a remote system and these files are getting processed and subsequently getting loaded to the database.
So there is a good chance to get the table space filled.
In my case I wanted to monitor both table space and disk space. Here is a perl script to do that.

#!/usr/bin/perl -w

#############################################################################################################
# NAME........... disk_table_space_monitor.pl
# AUTHOR......... govind kailas (govindkailas@gmail.com)
# DATE........... Mon Sep 12 17:15:54 2011
# PURPOSE........ a script to check the disk space,tablespaces and alert the concerned.
# ARGUMENT.......
# TO DO ......... provide the values for variables in the main block
#############################################################################################################

#=========================================================================================================================
#Feed the appropriate values to the variables in this block
$mail_dba=""; #Email alerts will be send to this id. eg :abc\@xyz.com , make sure to add a backslash infront of '@'
$dbname=""; #database name
$thershold=90; #default is 90 ,you can change it here if you want to
$LOG_FILE="$ENV{HOME}/Disk_Table_Results.txt"; #Where do you want to have your query result? default is current users home dir
#=========================================================================================================================

open LOG,">$LOG_FILE" or die"Cannot open $LOG_FILE $!";
chomp($dt_tm=`date`);
print LOG "$dt_tm\n";
print LOG "="x90,"\n\tDISK SPACE MONITOR\n";
print LOG "="x90,"\n";

#=========================================================================================================================
#Checking for free disk space
#=========================================================================================================================
$cmd = "df";
@sys_param= split /\s/,`uname -a`;
$sys_name=uc($sys_param[1]);
open(IN, "$cmd |") || die("Failed reading disk free(df)\n");
while () {
        chomp;
        next if /^Filesystem/ or /NFS server/;
        @column = split /\s+/;
        $column[3]=~s/%//g;
        #print "File system :$column[6] \n";
        next if  $column[3]=~/-/;
        print LOG "$sys_name Needs your attention - Usage of $column[6] is $column[3]%\n"if($column[3] > $thershold);

}
close IN;

print LOG "="x90,"\n\tTABLESPACE MONITOR\n";
print LOG "="x90,"\n";

#=========================================================================================================================
#Checking for table spaces
#=========================================================================================================================

`db2 connect to $dbname`; #connect to db, assumes the current user have the dba priv.
$rc = $?>>8;
if ($rc ne 0){print LOG "$sys_name - Unable to connect to DB $! !!\n";`mailx -s "Warning from $sys_name $mail_dba $tb_space_cmd="db2 -x \"SELECT SUBSTR(B.TABLESPACE_NAME,1,20) AS TABLESPACE,CHAR((B.TOTAL_PAGES*B.PAGE_SIZE/1048576)) AS TOTAL_SPACE_IN_MB,CHAR((B.USED_PAGES*B.PAGE_SIZE/1048576)) AS USED_SPACE_IN_MB,CHAR((B.FREE_PAGES*B.PAGE_SIZE/1048576)) AS FREE_SPACE_IN_MB,CHAR(B.USED_PAGES*100/B.TOTAL_PAGES) AS PERCENT_USED FROM TABLE (snapshot_tbs_cfg('$dbname',-1)) B WHERE TABLESPACE_TYPE=0 \"";
open (IN, "$tb_space_cmd |") || print LOG "DB Read Error $!\n";
while () {
        chomp;
        my($name,$avail,$used,$FREE_SPACE_IN_MB,$PERCENT_USED)=split(/\s+/,$_,5);
        $PERCENT_USED=~s/\s+$//;
        print LOG "$sys_name Needs your attention - Usage of TABLESPACE $name is $PERCENT_USED%\n" if $PERCENT_USED >$thershold;
}
close LOG;

`mailx -s "Warning from $sys_name" $mail_dba <$LOG_FILE`; #most of the unix based system supports mail

Make sure you are running this script with a DBA privileged id. If not the table space query will fail.
This script is intended to DB2 database only.

In the first block there are few variables left to be filled by the users. You must give values for at least the below mentioned variables
1) $mail_dba
2) $dbname

Below is an alert mail I received in my Gmail inbox

Compile a DB2 procedure using a perl script

I have been doing minor DB2 administration jobs for my team. I used to compile procedure from commandline using
db2 -td@ -vf proc_nm.spl >>proc_nm.spl.out
And after the successful compilation grant proper privileges to users.
db2 GRANT EXECUTE ON PROCEDURE proc_nm TO GROUP PRD_USR

What if we have some 30 procedures to compile ? It will be quite annoying to compile and grant access to each procedures. One easy way to do this is write a script which will take the procedure name as a parameter. I could have written it in shell but I am more comfortable in perl. So here the code goes ..

    compile_proc.pl


$file_nm=shift;
die "Pls provide the procedure file name" if $file_nm eq "";
print "File name $file_nm \n";
$proc_nm=`grep -i -e "DROP PROCEDURE" $file_nm`;
chomp $proc_nm;
print "Procedure to compile is $proc_nm \n";
die "DROP statement not found in $file_nm" if $proc_nm eq "";


$proc_nm=~s/DROP PROCEDURE (.*)@/$1/gi;
print "Extracted proc_nm is $proc_nm\n";


#=============================================================================================
#Connecting to the database
#=============================================================================================
$db2connect=`db2 connect to DBNAME`;
print "db2 connect value : $db2connect";

`db2 -td@ -vf $file_nm >> $file_nm.out`;
`echo "GRANT EXECUTE ON PROCEDURE $proc_nm TO GROUP GRP_NM" >>$file_nm.out`;
`db2 GRANT EXECUTE ON PROCEDURE $proc_nm TO GROUP GRP_NM >> $file_nm.out`;
print "$proc_nm compiled successfully !!\n";

its pretty simple, I am not using any modules to connect to DB2. Make sure that your shell is aware of db2.
I will explain it one by one.

The first statement is to receives the procedure file name and if the file name is not provided the script will exit giving a warning message.

$file_nm=shift;
die "Pls provide the procedure file name" if $file_nm eq "";
print "File name $file_nm \n";

Now we have to get the procedure name from the file.
$proc_nm=`grep -i -e "DROP PROCEDURE" $file_nm`;
chomp $proc_nm;
die "DROP statement not found in $file_nm" if $proc_nm eq "";

All my procedures will start with a drop statement, and our development team is very particular about it.
This makes our job easy, just grep the DROP statement from the file. What if we dont have a drop statement in the file ? In my case I will throw a warning and exit from the script. You may also check for the CREATE statement in case if you/team doesnt have the habit of writing a drop.


$proc_nm=~s/DROP PROCEDURE (.*)@/$1/gi;
print "Extracted proc_nm is $proc_nm\n";

the drop statement will look like this
DROP PROCEDURE PRD_COMMON.RECYCLE_ALLTYPE_PR@
So now we have to extract the procedure name, which is nothing but PRD_COMMON.RECYCLE_ALLTYPE_PR
Remove everything other than the procedure name thats what this statement is doing
$proc_nm=~s/DROP PROCEDURE (.*)@/$1/gi;

Now we can connect to the database and print the connect values from the database.

$db2connect=`db2 connect to DBNAME`;
print "db2 connect value : $db2connect";

So whats left ? yeah compile the procedure and give the privilege

`db2 -td@ -vf $file_nm >> $file_nm.out`;
`echo "GRANT EXECUTE ON PROCEDURE $proc_nm TO GROUP GRP_NM" >>$file_nm.out`;
`db2 GRANT EXECUTE ON PROCEDURE $proc_nm TO GROUP GRP_NM >> $file_nm.out`;
print "$proc_nm compiled successfully !!\n";

That’s it we are done !!

Give execute permission to the script and run.
Eg : ./compile_proc.plĀ  proc_nm.spl

Follow

Get every new post delivered to your Inbox.