Saturday, September 29, 2012

How could you update all Perl packages on Windows?

At command prompt, try :

ppm upgrade -install

Friday, September 21, 2012

How do you run MS SQL Server queries from command prompt?

Use either osql ( Oregon SQL ) or sqlcmd  command line utility.

What is the difference between exec and fork?

fork creates a new process by replicating itself (file descriptors, etc) to a subshell and runs the forked process in that subshell via exec. The new process acts as an independent process in separte memory space with its own unique process ID. It creates a Parent-Child relationship wherin the child returns its PID to parent and the parent provides zero on successful fork.

exec, on the other hand, executes the given process by replacing the current process. exec doesn't return any value on success. It shares the same memory space for the new process.

Tuesday, September 18, 2012

How do you insert data into the table of a database in Oracle in Perl?

use DBI;

$oracle_server = 'localhost';
$oracle_user = '<UserName>';
$oracle_passwd = '<Password>';
$oracle_sid = 'ORCL';
$oracle_table = 'PerlDB';
$oracle_port = '1521';

$dbh = DBI->connect("dbi:Oracle:host=$oracle_server; port=$oracle_port; sid=$oracle_sid", $oracle_user, $oracle_passwd, { RaiseError => 1, AutoCommit => 0}) or die "$DBI::errstr\n";

my $sql = "INSERT INTO $oracle_table (EMP_NUM, EMP_NM, JOB, MNGR_ID, SAL) VALUES (?, ?, ?, ?, ?)";

my $rv = $dbh->do($sql, undef, 21295, 'RABINDRA', 'PROGRAMMER', 7839, 8000) or die "$DBI::errstr";
$dbh->disconnect();

Monday, September 17, 2012

How do you connect Perl and Oracle on Windows?

#!C:/Perl/bin/perl

use DBI;

$oracle_server = 'localhost';
$oracle_user = '<UserName>';
$oracle_passwd = '<Password>';
$oracle_sid = '<SID>';
$oracle_table = 'EMP';
$oracle_port = '1521';

# Get a database handle by connecting to the database
$dbh = DBI->connect("dbi:Oracle:host=$oracle_server; port=$oracle_port; sid=$oracle_sid", $oracle_user, $oracle_passwd, { RaiseError => 1, AutoCommit => 1}) or die "$DBI::errstr\n";

my $sql = "SELECT E.EMPNO AS EMPLOYEE_ID,
   E.ENAME AS EMPLOYEE_NM,
   M.ENAME AS MNGR_NM
   FROM   $oracle_table E
   LEFT OUTER JOIN $oracle_table M
            ON E.MGR = M.EMPNO
   ORDER  BY E.EMPNO";

my $sth = $dbh->prepare($sql) or die "$DBI::errstr";
$sth->execute() or die "$! $?\n";

my $COL_NM = join("\t", @{$sth->{NAME}});
print "$COL_NM\n";

while (my @row = $sth->fetchrow_array())
{
    print join("\t", @row), "\n";
}

$sth->finish();
$dbh->disconnect();

How can you get column names of a table in Oracle?

SELECT column_name
FROM USER_TAB_COLUMNS
WHERE table_name = '<TableName>';

How could you find out all the table names having a specific column name in Oracle?

SELECT TABLE_NAME
FROM ALL_TAB_COLUMNS
WHERE COLUMN_NAME = '<ColumnName>';

SELECT TABLE_NAME
FROM USER_TAB_COLUMNS
WHERE COLUMN_NAME = '<ColumnName>';

SELECT TABLE_NAME
FROM DBA_TAB_COLUMNS
WHERE COLUMN_NAME = '<ColumnName>';

- USER_TAB_COLS for tables owned by the current user and ALL_TAB_COLS or DBA_TAB_COLS for tables owned by all users.

How can you get the First/Top N rows in Oracle?

select *
from <TableName>
where rownum <= n;

Saturday, September 15, 2012

How do you run shell commands transparently within Perl?

#!/usr/bin/perl
use Shell;

my $sh = Shell->new;
print $sh->ls(-l);

or

#!/usr/bin/perl

use Shell qw(ls who);
@fileList = ls ("-l");
@userList = who ("-m");
print @fileList;
print @userList;

How can you receive the output of an external command using open( ) in Perl?

#!C:/Perl/bin/perl -w
use strict;
open (PS, "dir/b |") || die "$!\n";
while ( <PS> )
{
   print "$_\n";
}
close (PS) or die $!;

Thursday, September 13, 2012

Write a batch file to kill frozen programs.

@echo off
taskkill /f /fistatus eq not responding
exit

- Copy the above 3 lines to Notepad and save the file as "killall.bat".
- On click, it kills all the programs that do not respond.

How do you remove duplicate lines in a file in UNIX without changing order of contents?

awk '!x[$0]++' <FileName> > <New FileName>

How do you create a pseudo drive or map a local folder to a pseudo drive in MS Windows?

Suppose, we want a folder (e.g. AutoDrive) to act as a pseudo drive. Now assume "Z:" to be used as Drive Letter, do either of the followings:

Click Start, choose Run and type:
SUBST Z: C:\AutoDrive
then click OK.
or
type "SUBST Z: C:\AutoDrive" at the command prompt without quotes.

Again, to remove the substituted "Z:" drive, follow either of the ways mentioned below:

click Start, choose Run and type:
SUBST Z: /d
or
type "SUBST Z: /d" at the command prompt without quotes.

Wednesday, September 5, 2012

How can you get basename of a Perl script on any OS using regular expression?

$me = $0;

if ( $^O eq 'MSWin32' )
{
         $me=~s{.*\\}{};
         print "Usage :  $me <file>!\n"
}
else
{
         $me=~s{.*/}{};
         print "Usage :  $me <file>!\n"
}

Or

my $command = $0;
$command =~ s#^.*/##;

Or

( my $me = $0 ) =~ s{.*(\\|/)}{};

Tuesday, September 4, 2012

How do you run the last command without the last argument?

use "!:-"

For example, if the following is the last command

sort -k2 <FileName>

then

!:- <New FileName>

is the same as

sort -k2 <New FileName>

How do you insert the result of a command into VI?

!!<COMMAND>

- will insert the result of the command into vi at the current position of the cursor.

How do you execute a command and kill itself after some duration?

timeout 5s <COMMAND>

-> Starts COMMAND, and kills itself after 5 seconds

How can you sort the values of a Hash alphabeticlly?

#!/usr/bin/perl
   
%FM = ( '91.1 '=> 'Radio City',
              '92.7' => 'Big FM',
              '93.5' => 'Red FM',
              '94.3' => 'Radio One',
              '98.3' => 'Radio Mirchi',
              '104.0' => 'Fever',
              '104.8' => 'Oye FM',
              '107.1' => 'FM Rainbow',
              '100.7' => 'FM Gold');

foreach $station (sort { $FM{$a} cmp $FM{$b} } keys %FM)
{
    print "$station is $FM{$station}.\n";
}

How can you sort the values of a Hash numerically?

#!/usr/bin/perl
   
%FM = ( 'Radio City' => '91.1',
              'Big FM' => '92.7',
              'Red FM' => '93.5',
              'Radio One' => '94.3',
              'Radio Mirchi' => '98.3',
              'Fever' => '104.0',
              'Oye FM' => '104.8',
              'FM Rainbow' => '107.1',
              'FM Gold' => '100.7');

foreach $station (sort { $FM {$a} <=> $FM {$b}} keys %FM )
{
        print "$station is $FM{$station} MHz.\n";
}

How can you sort the keys of a Hash alphabetically?

#!/usr/bin/perl
   
%FM = ( 'Radio City' => 91.1,
              'Big FM' => 92.7,
              'Red FM' => 93.5,
              'Radio One' => 94.3,
              'Radio Mirchi' => 98.3,
              'Fever' => 104.0,
              'Oye FM' => 104.8,
              'FM Rainbow' => 107.1,
              'FM Gold' => 100.7);

foreach $station (sort keys %FM)
{
    print "$station is $FM{$station} MHz.\n";
}

How can you sort the keys of a Hash numerically?

#!/usr/bin/perl

%FM = ( '91.1' => 'Radio City',
              '92.7' => 'Big FM',
              '93.5' => 'Red FM',
              '94.3' => 'Radio One',
              '98.3' => 'Radio Mirchi',
              '104.0' => 'Fever',
              '104.8' => 'Oye FM',
              '107.1' => 'FM Rainbow',
              '100.7' => 'FM Gold');

for $key ( sort {$a<=>$b} keys %FM )
{
        print "$key->$FM{$key}\n";
}

or

foreach ( sort { $a <=> $b } keys(%FM) )
{
    print "$_ = $FM{$_}\n";
}

How can you get the keys of a Hash in Perl?

#!/usr/bin/perl

%FM = ( 'Radio City' => 91.1,
              'Big FM' => 92.7,
              'Red FM' => 93.5,
              'Radio One' => 94.3,
              'Radio Mirchi' => 98.3,
              'FM Gold' =>100.7,
              'Fever' => 104.0,
              'Oye FM' => 104.8,
              'FM Rainbow' => 107.1);

#get keys only
foreach my $key (sort keys %FM)
{
        print "$key\n";
}

Write shell script to convert 10 to binary.

#!/bin/sh

binary=({0..1}{0..1}{0..1}{0..1})
echo ${binary[10]}

How could you delete all empty lines from a file within VI?

:g/^$/d
or
:g!/\S/d

Monday, September 3, 2012

How do you get the duplicate/repeated lines in a file in UNIX?

uniq -d <FileName> # print only those lines which are repeated

uniq -c <FileName> # adds the number of occurrences of each line

uniq -u <FileName> # print only those lines which are not at all repeated

What is the substitute to backticks?

"$( )" is the substitute to backticks (``) with an advantage of nesting commands without escapes and better readability. The only bottleneckness $( ) suffers from is lack of portability.

For Example,

VAR=`date +%D`
echo "The date is: $VAR"

or

echo "The date is: `date +%D`"

The above lines can be replaced with the following one :

echo "The date is: $(date +%D)"

How could you close shell keeping all subprocess running?

disown -a && exit

How do you empty or flush all contents of a file without removing it in UNIX?

Prefix the file with ">" and "space" at command line.

> <FileName>

How do you run a command without saving in the history in UNIX?

Prefixing one or more spaces to the command won't be saved in history.
<space> command

How do you wrap text file / line / words to fit in specified width in UNIX?

fold -sw {COUNT} {input.txt} > {output.txt}

Where,
-s: break at spaces.
-w: {COUNT} use COUNT as WIDTH columns instead of default 80.

For instance, following command will wrap input.txt at 70 width columns:

fold -sw 70 input.txt > output.txt

How do you sort a file on a particular column in UNIX?

sort -k2 <FileName>

will be sorted on column 2.

How do you preserve Ownership and Timestamp while copying files in UNIX?

cp -p <source> <destination>