Tuesday, May 12, 2015

Write a program to compare two files A and B and report the following: lines present in both files A and B = A U B, lines common to both A and B = A & B, lines present in A but not B = A - B, lines present in B but not in A = B - A.

#!/usr/local/bin/perl

##Script to compare 2 NEWS files
## Generates 3 files
## FILE_1_only: This file contains lines present only in first file
## FILE_2_only: This file contains lines present only in second file
## FILE1_2: This file contains lines common to both files

use strict;
use warnings;

my %hash1;
my %hash2;

##Use Tie module to process the hash in the insertion order ###
use Tie::IxHash;
tie %hash1, "Tie::IxHash";
tie %hash2, "Tie::IxHash";

###trim any leading or trailing spaces####
#sub rtrim { my $s = shift; $s =~ s/\s+$//; return $s };
sub trim { my $s = shift; $s =~ s/^\s+//; $s =~ s/\s+$//; return $s };

if(@ARGV != 2) {
        print "Please pass the files to be compared as command line arguments \n";
        print "Usage: $0 file1 file2 \n";
        print "Ex: $0 NEWS_5_0_1 NEWS_3_15_0 \n";
        print "O/P: FILE_1_only, FILE_2_only and FILE1_2 \n";
}

open (FH1, "<$ARGV[0]") or die "Couldnot open file $ARGV[0], $!";
open (FH2, "<$ARGV[1]") or die "Couldnot open file $ARGV[1], $!";
open (FH3, ">FILE1_2");
open (FH4, ">FILE_1_only");
open (FH5, ">FILE_2_only");

my @arr1=<FH1>;
my @arr2=<FH2>;

foreach my $l (@arr1) {
      chomp($l);
      $hash1{trim($l)}++;
}


foreach my $m (@arr2) {
      chomp($m);
      $hash2{trim($m)}++;
}

foreach my $k1 (keys %hash1) {
        if($hash2{$k1}) {
                print FH3 $k1;
                print FH3 "\n";
        } else {
                print FH4 $k1;
                print FH4 "\n";
        }
}

foreach my $k2 (keys %hash2) {
        if (!$hash1{$k2}) {
                print FH5 $k2;
                print FH5 "\n";
        }
}




Usage: ./compare_NEWS.pl NEWS_5.0.1 NEWS_3.15.0

O/P:
FILE_1_only: This file contains lines present only in first file
FILE_2_only: This file contains lines present only in second file
FILE1_2: This file contains lines common to both files

Write a program to accept username and password as input arguments without displaying the password.

#!/bin/bash

unset PASSWORD
unset CHARCOUNT

echo -n "Login:";
read login_name;
echo -n "Enter password: "

stty -echo

CHARCOUNT=0
while IFS= read -p "$PROMPT" -r -s -n 1 CHAR
do
    # Enter - accept password
     if [[ $CHAR == $'\0' ]] ; then
         break
     fi
     # Backspace
     if [[ $CHAR == $'\177' ]] ; then
         if [ $CHARCOUNT -gt 0 ] ; then
            CHARCOUNT=$((CHARCOUNT-1))
            PROMPT=$'\b \b'
            PASSWORD="${PASSWORD%?}"
         else
            PROMPT=''
         fi
     else
         CHARCOUNT=$((CHARCOUNT+1))
         PROMPT='*'
         PASSWORD+="$CHAR"
     fi
done
stty echo

if [[ $login_name == "pearl"  && $PASSWORD == "pearlyperl" ]] ; then
    echo -e "\n Login Successful  :-)  \n";
else
    echo -e "Login Failed \n";
fi


OUPUT:

./login.sh
Login:pearl
Enter password: **********
Login Successful :-)

Write a perl program to read username and password from user without displaying the password string.

#!/usr/local/bin/perl

use strict;
use warnings;

print "Please enter you credentials \n";
print "login:";
my $usr_nm = <STDIN>;
print "password:";
system('stty', '-echo');
my $pswd = <>;
system('stty', 'echo');
print "\n";




Output:
./login.pl
Please enter you credentials
login:user_name
password:

File the frequency of each word and print the output in the decreasing order

#!/usr/local/bin/perl

use strict;
use warnings;
my %count;
my $file = shift or die "Usage: $0 FILE\n";
open my $fh, '<', $file or die "Could not open '$file' $!";
while (my $line = <$fh>) {
        chomp $line;
        foreach my $str (split /\s+/, $line) {
             $count{$str}++;
        }
}

foreach my $str (reverse sort {$count{$a} <=> $count{$b}} keys %count) {
        printf "%-31s %s\n", $str, $count{$str};
}



USAGE: ./count.pl  txt1

OUTPUT:
are                               3
you                              3
Hii                               2
how                             2
hii                                2
i                                   1
Hoe                             1
am                               1
doing                           1
well                             1


Aptitude Question: Find the shortest distance travelled by a ant on the side of the cube to reach from A to H?












































The minimum distance travelled by ant to reach from A to H is = square root of 5 = 2.326

Sunday, May 10, 2015

What is @INC in perl?

The @INC array is a list of directories Perl searches when attempting to load modules. To display the current contents of the @INC array:

perl -e 'print @INC '

The following two methods may be used to append to Perl's @INC array:

1. Add the directory to the PERL5LIB environment variable.
2. Add use lib 'directory'; in your Perl script.

Installing latest version of python on CentOS 5

http://www.hosting.com/support/linux/installing-python-3-on-centosredhat-5x-from-source

The latest release of the python scripting language is Python 3.2. However due to backwards incompatibilities with Python 2, it has not been adopted for CentOS / Redhat Linux 5. T he primary reason for this, is because release of 'yum' (package management) used in EL5 requires Python 2. Because of this, Python cannot be upgraded in place to version 3 without breaking the package manager.
Therefore to use Python 3, it will need to be installed outside of /usr.

Installing

Below is the list of command (with inline comments) on what is required to compile and install Python 3 from source. The installation will be done into the prefix /opt/python3. This will ensure the installation does not conflict with system software installed into /usr.
# Install required build dependencies
yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel

# Fetch and extract source. Please refer to http://www.python.org/download/releases
# to ensure the latest source is used.
wget http://www.python.org/ftp/python/3.2/Python-3.2.tar.bz2
tar -xjf Python-3.2.tar.bz2
cd Python-3.2

# Configure the build with a prefix (install dir) of /opt/python3, compile, and install.
./configure --prefix=/opt/python3
make
sudo make install
Python 3 will now be installed to /opt/python3.
$ /opt/python3/bin/python3 -V
Python 3.2
Ensure your python 3 scripts and applications query the correct interpreter.
#!/opt/python3/bin/python3