Wednesday, August 29, 2012

How could you convert a flat file to XML in Perl?

#!/usr/bin/perl

if ($#ARGV < 0)
{
 print "Usage : $0 <FlatFile Name>\n";
 exit;
}

my $in_txt=shift @ARGV;
my $out_xml=$in_txt;
$out_xml=~s/\.txt/\.xml/g;

open(INFO,"<$in_txt");
open(XML,">$out_xml");

print XML "<\?xml version=\"1.0\" encoding=\"UTF-16\"\?>\n";
print XML "<employee>\n";
while(<INFO>)
{
 if (/\A(.*?),(\d*+),(.*?),(\d*+),(\d*-\d*-\d+)/is)
 {
  print XML "<Employee_Detail>\n\t<Name>$1<\/Name>\n\t<Id>$2<\/Id>\n\t<State>$3<\/State>\n\t<Extension>$4<\/Extension>\n\t<Mobile>$5<\/Mobile>\n<\/Employee_Detail>\n";
 }
}
print XML "<\/employee>";
print "\"$in_txt\" got successfully converted to \"$out_xml\"";

employee.txt - The Flat File
------------------------------------------------------------------
Rabindra Nayak,23371,New Jercy,2370,212-555-6868
Mark Perino,23750,New York,2459,212-555-7979
Adam Gill,18654,DC,3897,212-555-5757
Tim Anderson,23456,Virginia,6543,212-555-3030
Mike Gotham,22500,Phoenix,7856,212-555-8989

Tuesday, August 28, 2012

Write a shell script to print itself.

#!/bin/sh

cat $0

How do you kill a process tree in UNIX?

Sometimes processes tend to re-spawn after being killed even if the parent process is killed.
The command below will kill all of the process (e.g. httpd process) :

ps -ef | grep httpd | awk '{print $2}' | xargs kill -9

How do you create temporary tables in Oracle?

Oracle database allows us to create Temporary tables of two types:

1- Transaction Specific Temporary Table
2- Session Specific Temporary Table

A transaction specific temporary table holds data when a transaction begins with execution of first SQL statement and ends either by commit or rollback. The following command creates a transaction specific temporary table.

CREATE global TEMPORARY TABLE temp_table
  (
     emp_id   NUMBER,
     emp_name VARCHAR2(50)
  )
ON COMMIT DELETE ROWS;


Note :- If "ON COMMIT" clause is omitted, by default oracle creates a transaction specific temporary table.

A session specific temporary table holds data until session lasts. When a commit is performed on session specific temporary table, data is preserved in table. But the data is not visible to others session. The data is lost when session ends.

Session specific temporary table can be created using “PRESERVE ROWS” in ON COMMIT clause.

CREATE global TEMPORARY TABLE temp_table
  (
     emp_id   NUMBER,
     emp_name VARCHAR2(50)
  )
ON COMMIT preserve ROWS;


The definition of temporary table is visible to all the sessions. Unlike permanent table, segment for temporary table is allocated when first INSERT statement is executed on temporary table. Indexes can also be created on temporary tables. The scope and life time of these indexes is similar to temporary tables.

Monday, August 27, 2012

How do you create temp tables in Sybase?

You can create Sybase temp tables in 2 ways :

1. CREATE TABLE #TableName
2. CREATE TABLE tempdb..TableName

Temp table being created with "#" are accessible by the current session or procedure and the table gets  automatically dropped when the session/procedure ends or is dropped manually.

Table being created using "tempdb..TableName" resides in tempdb database and remains as long as not dropped in manually. It is shareable among all sessions.

** If a table is created in any database other than tempdb, it won't be a temp table.

How could you determine the directory a shell script is in?

#!/usr/bin/sh -f

_DIR=`dirname "$(readlink -f $0)"`
echo ${_DIR}

Write a self-terminating shell script which doesn't hang up indefinately.

#!/usr/bin/sh

( sleep 5 ; echo Warning $0 hanging - aborted ; kill -9 $$ ) &
TIMEOUT_PID=$!

Your_Line1 ...
Your_Line2 ...
Your_Line3 ...
Your_Line4 ...
Your_Line5 ... 

# kill supervision sub-process
kill -9 $TIMEOUT_PID

How do you get 2nd, 3rd column of 2nd, 3rd row of CSV file?


head -3  <FileName> | tail -2 | cut -d "," -f 2-3
or
cat <FileName> | sed -n '2,3p' | cut -d "," -f 2-3
or
cat <FileName> | awk 'NR==2,NR==3' | cut -d "," -f 2-3

How could you read a file line by line in UNIX?

#!/usr/bin/sh

for line in $(cat <FileName>)
do
    echo "$line"
done

or

#!/bin/sh

file="<FileName>"

while read line
do
    echo $line
done < $file