Wednesday, December 19, 2012

Perl - Return a value from function depending on the context

 How to return a value from a function depending on the context? For example, if the return value is collected into an array, the function should return an array, if collected in a scalar, then the return value should be a specific scalar. Let us see how to handle this situation in Perl.

  The Perl script below uses a function "calc" which for a given array as input, calculates the sum of all elements in the array, and also finds the biggest and smallest  number in the array.

Monday, December 17, 2012

Shell Script - How to dump a Oracle table into a CSV file?

How to dump /export the contents of an Oracle table into a CSV file? Let us write a shell script to dump the content of EMP table into a CSV file.

Wednesday, December 12, 2012

How to print every nth line in a file in Linux?

How to print every nth line of a file?  The requirement is to print every 3rd line of the file.
  Let us consider a file with the following content.
$ cat file
AIX
Solaris
Unix
Linux
HPUX
Ubuntu
1. Using awk :
$ awk '!(NR%3)' file
Unix
Ubuntu
  NR variable contains the line number of the file in awk. If NR modulus 3 is equal to 0, it indicates the line is a multiple of 3. And only those lines are printed which modulus is 0.

Tuesday, December 11, 2012

sed - 10 examples to print lines from a file

 In this article of sed series, we will see how to print a particular line using the print(p) command of sed.
  Let us consider a file with the following contents:
$ cat file
AIX
Solaris
Unix
Linux
HPUX
1. Print only the first line of the file:
$ sed -n '1p' file
AIX
  Similarly, to print a particular line, put the line number before 'p'.

2. Print only the last line of the file
$ sed -n '$p' file
HPUX
 $ indicates the last line.

Wednesday, December 5, 2012

10 examples of initializing a Hash variable in Perl

 Hash'es  is one of the most important concepts in Perl. Hashes are associative arrays where the data is stored in the form of key-value pairs. The big advantage of a hash is faster look-up compared to arrays. In this article, we will see the how we can populate / initialize a hash variable in different scenarios:

1. Regular way of initializing a hash :
my %h1=("Jan" => 31, "Feb" => 28, "Mar" => 31);
  This is the normal way of populating hash where-in every set is a key-value pair. The operator separating the key-value pair '=>' is called Fat-comma operator.
  After the initialization, the data in the hash will be stored as shown below where 'Jan','Feb' and 'Mar' are the keys of the hash and values against it are their respective values associated.
'Jan' => 31
'Feb' => 28
'Mar' => 31

Monday, December 3, 2012

How to remove the leading and trailing spaces in a file?

How to remove / delete the leading and trailing spaces in a file? How to replace a group of spaces with a single space?
    Let us consider a file with the below content:
$ cat file
 Linux  25
Fedora   40
Suse 36
    CentOS 50
LinuxMint 15
Using the -e option of cat, the trailing spaces can be noticed easily(the $ symbol indicates end of line)
$ cat -e file
 Linux  25$
Fedora   40$
Suse 36    $
    CentOS 50$
LinuxMint 15$