Monday, September 3, 2012

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>

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.