Wednesday, 5 February 2014

File comparison

cmp filename1 filename2

cmp -- is one of the four principle UNIX file comparison utilities.
   It compares 2 files, and returns the positions where they differ.

comm -options filename1 filename2


comm -- This utility is used in comparing two files, produces three
   columns of output. The first contains lines unique to the
   first file, the second, lines unique to the second, and the
   third column, lines common to both files. By placing the
   numbers [1], [2], and/or [3] in the [options] position, any
   one (or more) of these columns can be suppressed.

To compare and scan large files

bdiff      Compares two large files.

bfs        Scans a large file to determine where to split
           it into smaller files.

To print out basic info on an account

finger

Format: finger username

To automatically correct mistakes in directory names

Use shopt -s cdspell to correct the mistakes in the cd command automatically.

cd /etc/mall
-bash: cd: /etc/mall: No such file or directory

# shopt -s cdspell
# cd /etc/mall
# pwd
/etc/mail

Monday, 3 February 2014

To find duplicate and distinct records

1. To display the number of occurrences and the records.

sort f1.txt | uniq -c
2 class
3 jar
1 bin

2.To display only the duplicate records.

sort f1.txt | uniq -d
class
jar

3.To display the distinct records

sort f1.txt | uniq
or
sort f1.txt | uniq -u

bin

Sunday, 2 February 2014

To remove duplicate lines in the first file1.txt and output the results to the second file.

uniq myfile1.txt > myfile2.txt

syntax :
uniq [option] filename
 
The options of uniq command are:

  • c : Count of occurrence of each line.
  • d : Prints only duplicate lines.
  • D : Print all duplicate lines
  • f : Avoid comparing first N fields.
  • i : Ignore case when comparing.
  • s : Avoid comparing first N characters.
  • u : Prints only unique lines.
  • w : Compare no more than N characters in lines
The default behavior of the uniq command is to suppress the 
duplicate line. Note that, you have to pass sorted input to 
the uniq, as it compares only successive lines. 
 
If the lines in the file are not in sorted order, then use the 
sort command and then pipe the output to the uniq command. 

> sort example.txt | uniq
 

Thursday, 23 January 2014

Anonymous Class

An anonymous class in Java is a class not given a name and is both declared and instantiated in a single statement. You should consider using an anonymous class whenever you need to create a class that will be instantiated only once.
Although an anonymous class can be complex, the syntax of anonymous class declarations makes them most suitable for small classes that have just a few simple methods.
An anonymous class must always implement an interface or extend an abstract class. However, you don’t use the extends or implements keyword to create an anonymous class. Instead, you use the following syntax to declare and instantiate an anonymous class:
new interface-or-class-name() { class-body }
Within the class body, you must provide an implementation for each abstract method defined by the interface or abstract class. Here’s an example that implements an interface named runnable, which defines a single method named run:
runnable r = new runnable()
    {
        public void run()
        {
            //code for the run method goes here
        }
    };
Here are a few other important facts concerning anonymous classes:
  • An anonymous class cannot have a constructor. Thus, you cannot pass parameters to an anonymous class when you instantiate it.
  • An anonymous class can access any variables visible to the block within which the anonymous class is declared, including local variables.
  • An anonymous class can also access methods of the class that contains it.

Wednesday, 22 January 2014

Command to list the file name in the directory which contains the specific word

directory > grep word .*

CUT command in Unix

Cut command in unix (or linux) is used to select sections of text from each line of files.The cut command can be used to print characters in a line by specifying the position of the characters. 

1.To print the characters in a line, use the -c option in cut command

Example:
> cat file.txt
unix or linux
is unix good
is linux good

cut command to print characters by position:

cut -c4 file.txt
x
u
l

2.cut command to print characters by range
cut -c4-7 file.txt
prints the character from 4th position to 7th position.

cut -c-6 file.txt
prints  the character from first  upto 6th.

cut -c10- fil.txt
prints the character from 10th position to till the end.

Types of polymorphism

In java language, polymorphism is essentially considered into two versions.
  1. Compile time polymorphism (static binding or method overloading)
  2. Runtime polymorphism (dynamic binding or method overriding)
Here, it is important to understand the these divisions are specific to java. In context to software engineering, there are other form of polymorphisms also applicable to different languages, but for java, these are mainly considered.

Compile time polymorphism (static binding or method overloading)

As the meaning is implicit, this is used to write the program in such a way, that flow of control is decided in compile time itself. It is achieved using method overloading.
In method overloading, an object can have two or more methods with same name, BUT, with their method parameters different. These parameters may be different on two bases:
1) Parameter type: Type of method parameters can be different. e.g. java.util.Math.max() function comes with following versions:
public static double Math.max(double a, double b){..}
public static float Math.max(float a, float b){..}
static long Math.max(long a, long b){..}
public static int Math.max(int a, int b){..}
The actual method to be called is decided on compile time based on parameters passed to function in program.
2) Parameter count: Functions accepting different number of parameters. e.g. in employee management application, a factory can have these methods:
EmployeeFactory.create(String firstName, String lastName){...}
EmployeeFactory.create(Integer id, String firstName, String lastName){...}
Both methods have same name “create” but actual method invoked will be based on parameters passed in program.

Runtime polymorphism (dynamic binding or method overriding)

Runtime polymorphism is essentially referred as method overriding. Method overriding is a feature which you get when you implement inheritance in your program.
A simple example can be from real world e.g. Animal. An application can have Animal class, and its specialized sub classes like Cat and Dog. These subclasses will override the default behavior provided by Animal class + some of its own specific behavior.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Animal {
    public void makeNoise()
    {
        System.out.println("Some sound");
    }
}
class Dog extends Animal{
    public void makeNoise()
    {
        System.out.println("Bark");
    }
}
class Cat extends Animal{
    public void makeNoise()
    {
        System.out.println("Meawoo");
    }
}
Now which makeNoise() method will be called, depends on type of actual instance created on runtime e.g.
?
1
2
3
4
5
6
7
8
9
10
public class Demo
{
    public static void main(String[] args) {
        Animal a1 = new Cat();
        a1.makeNoise(); //Prints Meowoo
         
        Animal a2 = new Dog();
        a2.makeNoise(); //Prints Bark
    }
}

Important points:

  1. Polymorphism is the ability to create a variable, a function, or an object that has more than one form.
  2. In java, polymorphism is divided into two parts : method overloading and method overriding.
  3. Some may argue that method overloading is not polymorphism. Then what does the term compile time “polymorphism” means??
  4. Another term operator overloading is also there, e.g. “+” operator can be used to add two integers as well as concat two sub-strings. Well, this is the only available support for operator overloading in java, and you can not have your own custom defined operator overloading in java.