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.