Friday, May 17, 2013

Retrieving a List of files from Remote Server

Below is the perl script for retrieving the list of files from Remote server:

    #!/usr/bin/perl
    use strict;
    use warnings;
    use Net::FTP;
    my $ftp_site     = 'localhost';
    my $ftp_dir      = '/home/simon/software';
    my $ftp_user     = 'a_login_name';
    my $ftp_password = 'a_password';
    my $glob         = 'ex*';
    my @remote_files;
    my $ftp = Net::FTP->new($ftp_site)
        or die "Could not connect to $ftp_site: $!";
    $ftp->login($ftp_user, $ftp_password)
        or die "Could not login to $ftp_site with user $ftp_user: $!";
    $ftp->cwd($ftp_dir)
        or die "Could not change remote working " .
                 "directory to $ftp_dir on $ftp_site";
    @remote_files = $ftp->ls($glob);
    foreach my $file (@remote_files) {
        print "Saw: $file\n";
    }
    $ftp->quit();

Wednesday, May 15, 2013

List of Bash Conditional statements

At times you need to specify different courses of action to be taken in a shell script, depending on the success or failure of a command. The if construction allows you to specify such conditions.

The most compact syntax of the if command is:
if TEST-COMMANDS; then CONSEQUENT-COMMANDS; fi

The TEST-COMMAND list is executed, and if its return status is zero, the CONSEQUENT-COMMANDS list is executed. The return status is the exit status of the last command executed, or zero if no condition tested true.
The TEST-COMMAND often involves numerical or string comparison tests, but it can also be any command that returns a status of zero when it succeeds and some other status when it fails. Unary expressions are often used to examine the status of a file. If the FILE argument to one of the primaries is of the form /dev/fd/N, then file descriptor "N" is checked. stdin, stdout and stderr and their respective file descriptors may also be used for tests.

Expressions used with if
The table below contains an overview of the so-called "primaries" that make up the TEST-COMMAND command or list of commands. These primaries are put between square brackets to indicate the test of a conditional expression.

Primary expressions
[ -a FILE ] True if FILE exists.
[ -b FILE ] True if FILE exists and is a block-special file.
[ -c FILE ] True if FILE exists and is a character-special file.
[ -d FILE ] True if FILE exists and is a directory.
[ -e FILE ] True if FILE exists.
[ -f FILE ] True if FILE exists and is a regular file.
[ -g FILE ] True if FILE exists and its SGID bit is set.
[ -h FILE ] True if FILE exists and is a symbolic link.
[ -k FILE ] True if FILE exists and its sticky bit is set.
[ -p FILE ] True if FILE exists and is a named pipe (FIFO).
[ -r FILE ] True if FILE exists and is readable.
[ -s FILE ] True if FILE exists and has a size greater than zero.
[ -t FD ]   True if file descriptor FD is open and refers to a terminal.
[ -u FILE ] True if FILE exists and its SUID (set user ID) bit is set.
[ -w FILE ] True if FILE exists and is writable.
[ -x FILE ] True if FILE exists and is executable.
[ -O FILE ] True if FILE exists and is owned by the effective user ID.
[ -G FILE ] True if FILE exists and is owned by the effective group ID.
[ -L FILE ] True if FILE exists and is a symbolic link.
[ -N FILE ] True if FILE exists and has been modified since it was last read.
[ -S FILE ] True if FILE exists and is a socket.
[ FILE1 -nt FILE2 ] True if FILE1 has been changed more recently than FILE2, or if FILE1 exists and FILE2 does not.
[ FILE1 -ot FILE2 ] True if FILE1 is older than FILE2, or is FILE2 exists and FILE1 does not.
[ FILE1 -ef FILE2 ] True if FILE1 and FILE2 refer to the same device and inode numbers.
[ -o OPTIONNAME ] True if shell option "OPTIONNAME" is enabled.
[ -z STRING ] True of the length if "STRING" is zero.
[ -n STRING ] or [ STRING ] True if the length of "STRING" is non-zero.
[ STRING1 == STRING2 ]  True if the strings are equal. "=" may be used instead of "==" for strict POSIX compliance.
[ STRING1 != STRING2 ]  True if the strings are not equal.
[ STRING1 < STRING2 ]  True if "STRING1" sorts before "STRING2" lexicographically in the current locale.
[ STRING1 > STRING2 ]  True if "STRING1" sorts after "STRING2" lexicographically in the current locale.
[ ARG1 OP ARG2 ] "OP" is one of -eq, -ne, -lt, -le, -gt or -ge. These arithmetic binary operators return true if "ARG1" is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to "ARG2", respectively. "ARG1" and "ARG2" are integers.

Expressions may be combined using the following operators, listed in decreasing order of precedence:
Combining expressions
[ ! EXPR ]          True if EXPR is false.
[ ( EXPR ) ]        Returns the value of EXPR. This may be used to override the normal precedence of operators.
[ EXPR1 -a EXPR2 ]  True if both EXPR1 and EXPR2 are true.
[ EXPR1 -o EXPR2 ]  True if either EXPR1 or EXPR2 is true.

The [ (or test) built-in evaluates conditional expressions using a set of rules based on the number of arguments. More information about this subject can be found in the Bash documentation. Just like the if is closed with fi, the opening square bracket should be closed after the conditions have been listed.

Monday, May 13, 2013

Semicolon is optional in perl

I guess most of the people have noticed about this and have just ignored it.
Or they must not have noticed it at all.
Do you know this fact about perl:
If you write this :
for(@arr)
{
print $_
}
Its not an error in perl.
DO remember this taken from perldoc:
Every simple statement must be terminated with a semicolon, unless it is the final statement in a block, in which case the semicolon is optional.

Thursday, May 9, 2013

Row width and Column separator in Sybase isql

Hi Again people,

Probably many of you either donot know or perhaps you are not aware of these since there is very less documentsation available in google search for these :)

In general I use:
isql -U<user> -P<passsword>  -D<dbname>

There are two ioptions in isql.
which are
-s ->column separator.This wil separate columns with what ever character is followed by this option
another option is (and probably very important):
-w-> this will set the row width.By default is set to 80 characters and if the row length exceeds 80 characters, a new line is added in the console output.
 
for eg:
isql -U<user> -P<passsword> -D<dbname> -s ',' -w 65535

Tuesday, May 7, 2013

Consequtive match of a pattern multiple times in perl

Make a pattern that will match three consecutive copies of whatever is currently contained in $what. That is, if $what is fred, your pattern should match "fredfredfred". If $what is "fred|barney", your pattern should match fredfredbarney, barneyfredfred, barneybarneybarney, or many other variations.

Well, This is quite tricky. But the magic match to do over here is :
/($str){3}/
Below is the example code:
#!/usr/bin/perl
use warnings;
use strict;
# notice the example has the `|`. Meaning
# match "fred" or "barney" 3 times.
my $str = 'fred|barney';
my @tests = qw(fred fredfredfred barney barneybarneybarny barneyfredbarney);
for my $test (@tests) {
if( $test =~ /^($str){3}$/ ) {
print "$test matched!\n";
} else {
print "$test did not match!\n";
}
}
Below is the execution:
> ./temp9.pl
fred did not match!
fredfredfred matched!
barney did not match!
barneybarneybarny did not match!
barneyfredbarney matched!
>

Thursday, May 2, 2013

How does free/delete in C/C++ know how much memory to free/delete

This is probably very important for every C/C++ programmer to know.
And most of the interviewers too ask this question.I myself had asked in many interviews, but hardly I can get a good response from the people.

So I thought it would be better that post it here so that people will get to know.
char *ptr =malloc(5*sizeof(char));
The above statement will allocate 5 bytes of memory for ptr on heap.
I had written the word heap in bold, since most people will think that memory is allocated on stack.

So after you use ptr in your code and get the task done you have to delete or free the memory.
so you write this for it.
free(ptr);
Remember, you are not passing 5 bytes to free as an argument, but how come free function know that it has to free 5 bytes?

When you call malloc(), you specify the amount of memory to allocate. The amount of memory actually used is slightly more than this, and includes extra information that records (at least) how big the block is. You can't (reliably) access that other information - and nor should you :-).
When you call free(), it simply looks at the extra information to find out how big the block is.

This is the logic behind this magic(atleast for a programmer this is a magic provided by the standard).

also see the link in c-faqs:
How does free know how many bytes to free?


Same principle is followed for delete and new.


Tuesday, April 30, 2013

Replace a string in a file with the value in environment variable.

Many times its required inside a shell script that you need to replace a string in a file with a value which is present in a environment variable.

Most of the time people will face this in thier daily work life if they work on unix specially.

Well below is trich to do it.
echo $LINE | sed -e "s/12345678/${replace}/g"

Small test below:
> export replace=987654321
> echo X123456789X | sed "s/123456789/${replace}/"
X987654321X

Thursday, April 25, 2013

C++ overcome the definition inthe standard libraries

I have a function abs in one of the libraries and i wanted to use that function by linking to that library.
But i face this error below:
function abs used is #define abs(x) (x &gt; 0) ? x : -(x).

This is obviously from the stadard includes that i used.
So i found a very nice way to overcome this like below:
(abs)(arguments)

Wrapping the function name abs in paranthesis has resolved the problem right away.
Well, I also found one more way to achieve the same thing.
use
#undef abs

Before you call the function abc in the library.

Tuesday, April 23, 2013

Print Lines which have some unique fields in awk

I have a table in which most of the values in a given row are the same. What I want to pull out are any rows where at least one of the values is different.

For eg i have a file:

$ cat file
one one one one two
two two two two two
three four four five
I found a nice trick for doing this:

$ awk '{f=$0;gsub($1,"")}NF{print f}' file
one one one one two
three four four five

First we store the line in original state f=$0 then we do a global substitution on everything matching the first field, if all fields are the same then nothing will be left therefor NF will be 0 and nothing will be printed else we print the original line.

Wednesday, April 17, 2013

Sorting a file with related lines

I have a file in which first row contains the number and second row contains a statement associated with it and so on like the below example
File
-----------
12
stat1
18
stat2
15
stat3


 
I need to print the output like sorting reversely as per numbers and so the statement related to it
Output needed is :
----------------------
Time = 18
Stat = stat2
Time = 15
Stat = stat3
Time = 12
Stat = stat1
 
 
Below is the perl command that i have written which does the needed thing.
 
> perl -lne 'if(/^\d+/){$k=$_}else{$x{$k}=$_}END{for(sort {$b<=>$a} keys %x){print "time=$_\nStat=$x{$_}";}}' file
time=18
Stat=stat2
time=15
Stat=stat3
time=12
Stat=stat1
>


 
 
 
Logic:
-------
precondition is time will come first and then the stetement associated with it.
1. so store in a variable $k if the line start with a decimal point number.
2. Else case it is nothing but the statement. So stroe this line as a value to the previous stored $k.
3. After parsing complete file.sort the keys of the hash in descending order by
   sort  $b<=>$a and then print each and ever key and value in needed format.
Note:
-----
1. sort $a<=>$b will sort the numbers in ascending order.
2. sort $b<=>$a will sort the numbers in descending order
 

 

Friday, March 29, 2013

Column wise comaprision using awk

There was a requirement once where i need to compare two files not with thier rows instead  i needed to do the comparision with columns.
I wanted only those rows where any of the columns in the lines differ.

for eg:
File1
1 A B C D
2 E F G H
File2
1 A Z C D
2 E F Y H
3 M N O P
Below is the Output I need:
file1 1 col2 B
file2 1 col2 Z
file1 2 col3 G
file2 2 col3 Y

Below is the solution in awk that i have written.
awk 'FNR==NR{a[FNR]=$0;next} {
if(a[FNR])
{split(a[FNR],b);
for(i=1;i<=NF;i++)
{
if($i!=b[i])
{
printf "file1 "b[1]" col"b[i-1]" "b[i]"\n";
printf "file2 "$1" col"b[i-1]" "$i"\n";
}
}
}
}'
Below is the test i made on my solaris server:


> nawk 'FNR==NR{a[FNR]=$0;next}{if(a[FNR]){split(a[FNR],b);for(i=1;i<=NF;i++){if($i!=b[i]){printf "file1 "b[1]" col"i-1" "b[i]"\n";printf "file2 "$1" col"i-1" "$i"\n";}}}}' file1 file2
file1 1 col2 B
file2 1 col2 Z
file1 2 col3 G
file2 2 col3 Y
>

Tuesday, March 26, 2013

Increment a number for each repeat

I have a file where column one has a list of family identifiers
AB
AB
AB
AB
SAR
SAR
EAR
Is there a way that I can create a new column where each repeat is numbered creating a new label for each repeat i.e.
AB_1
AB_2
AB_3
AB_4
SAR_1
SAR_2
EAR_1
Below is a pretty simple solution for this:
awk '{print $1"_"++a[$1]}' file

Converting single column to multiple columns

I have a file which contains all the entries in a single column like:

0
SYSCATSPACE
16384
13432
2948
1
1
TEMPSPACE1
1
1
applicable
1
2
USERSPACE1
4096
1888
2176
1
 
If  I want to convert this in a tabular form of 3*6 :

0 SYSCATSPACE 16384 13432 2948       1
1 TEMPSPACE1  1     1     applicable 1
2 USERSPACE1  4096  1888  2176       1
 
Below is the command that i will use:
 
perl -lne '$a.="$_ ";if($.%6==0){push(@x,$a);$a=""}END{for(@x){print $_}}' your_file
 
output would be :
> perl -lne '$a.="$_ ";if($.%6==0){push(@x,$a);$a=""}END{for(@x){print $_}}' temp
0 SYSCATSPACE 16384 13432 2948 1
1 TEMPSPACE1 1 1 applicable 1
2 USERSPACE1 4096 1888 2176 1

Tuesday, March 12, 2013

Capture all the letters which are repeated more than once

Recently i came across a need where i need to fetch all the letters in a line which are repeated more than once in a line contiguously.

for example :

lets say there a word "foo bar". I want the letter 'o' in this.
lets say there a word "foo baaar". I want the letters 'o','a' in this.
lets say there a word "foo baaar foo". I want the letters 'o','a' in this again.

Below is code which worked for me:

perl -lne 'push @a,/(\w)\1+/g;END{print @a}' your_file

The above command will scan complete file and prints just the letters that are repeated continguously in the file.

Friday, February 22, 2013

Summing up column values based upon ranges in another file

File 1 has ranges 3-9, 2-6 etc
    3 9
    2 6
    12 20

File2 has values: column 1 indicates the range and column 2 has values.
    1 4
    2 4
    3 5
    4 4
    5 4
    6 1
    7 1
    8 1
    9 4

I would like to calculate the sum of values (file2, column2) for ranges in file1). Eg: If range is 3-9, then sum of values will be 5+4+4+1+1+1+4 = 20

Below is the script for doing the same:

use strict;
use warnings;
use feature qw( say );

use List::Util qw( sum );
my $file1 = 'file1.txt';
my $file2 = 'file2.txt';

my @file2;
{   
   open(my $fh, '<', $file2)
      or die "Can't open $file2: $!\n";
   while (<$fh>) {
      my ($k, $v) = split;
      $file2[$k] = $v;
   }
}

{   
   open(my $fh, '<', $file1)
      or die "Can't open $file1: $!\n";
   while (<$fh>) {
      my ($start, $end) = split;
      say sum grep defined, @file2[$start .. $end];
   }
}

Monday, February 18, 2013

Capitalize every first letter of a word and others to lower case

How can you capitalize(Only first character) each word in a line using perl and change remaining to lower case?

let say there is a file:
> cat temp
hI thIs is woRld
How can we make it
Hi This Is World
Its very simple to do it in perl:
perl -pe 's/(\w+)/\u\L$1/g' your_file
If you want to do it inplace just add a -i flag:
perl -pi -e 's/(\w+)/\u\L$1/g' your_file

Wednesday, February 13, 2013

Print the next power of 2 for a given number

Below is the program that I have written for printing the next power of 2 for a given number.
The code mostly uses  the bitwise operations in c/c++.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>


int main ()
{
int num;
scanf("%d",&num);
int count=0;

        if(!((num != 1) && (num&(num-1))))
        {
        printf("%d is already a power of two.\n",num);
        printf("%d is next number power of 2.\n",num*2);
        }
        else
        {
        while((num>>=1)&&(++count));
        printf("%0.0f",pow(2.0,count+1));
        }

}

Thursday, February 7, 2013

Sorting files based upon the numeric value before the first field separator

Lets say i have a list of files.

for eg:

ABCD5_Avgsdfgsg.txt
ABCD7_ff.txt
CD8_PKG_CV.txt
ABCD1_11.txt
BCD2_fbefjf.txt
D3_C.txt
ABCD4_123.txt
ABCD10_123.txt


I want to sort these files according to the first number before the first underscore.
I tried with sort but with no proper solution then i tried it in perl and this worked.Hopefully the below helps.

ls -1 | perl -F"_" -lane '$F[0]=~s/[a-zA-Z]//g;$X{$F[0]}=$_;END{foreach(sort {$a<=>$b} keys(%X)){print $X{$_}}}'

tested below:

> ls -1
ABCD10_123.txt
ABCD1_11.txt
ABCD4_123.txt
ABCD5_Avgsdfgsg.txt
ABCD7_ff.txt
BCD2_fbefjf.txt
CD8_PKG_CV.txt
D3_C.txt



> ls -1 | perl -F"_" -lane '$F[0]=~s/[a-zA-Z]//g;$X{$F[0]}=$_;END{foreach(sort {$a<=>$b} keys(%X)){print $X{$_}}}'
ABCD1_11.txt
BCD2_fbefjf.txt
D3_C.txt
ABCD4_123.txt
ABCD5_Avgsdfgsg.txt
ABCD7_ff.txt
CD8_PKG_CV.txt
ABCD10_123.txt
>

Thursday, January 31, 2013

Creating a child process in perl

This example fork 10 child processes.
It will wait for all child's to finish before exiting.


#!/usr/local/bin/perl

use strict;
use warnings;

print "Starting main program\n";
my @childs;

for ( my $count = 1; $count <= 10; $count++) {
        my $pid = fork();
        if ($pid) {
        # parent
        #print "pid is $pid, parent $$\n";
        push(@childs, $pid);
        } elsif ($pid == 0) {
                # child
                sub1($count);
                exit 0;
        } else {
                die "couldnt fork: $!\n";
        }
}
foreach (@childs) {
        my $tmp = waitpid($_, 0);
         print "done with pid $tmp\n";

}
print "End of main program\n";

sub sub1 {
        my $num = shift;
        print "started child process for  $num\n";
        sleep $num;
        print "done with child process for $num\n";
        return $num;
}


Output looks like:

Starting main program
started child process for  1
started child process for  2
started child process for  3
started child process for  4
started child process for  5
started child process for  6
started child process for  9
started child process for  10
started child process for  7
started child process for  8
done with child process for 1
done with pid 5584
done with child process for 2
done with pid 5585
done with child process for 3
done with pid 5586
done with child process for 4
done with pid 5587
done with child process for 5
done with pid 5588
done with child process for 6
done with pid 5589
done with child process for 7
done with pid 5590
done with child process for 8
done with pid 5591
done with child process for 9
done with pid 5593
done with child process for 10
done with pid 5594
End of main program

Explicit keyword in c++

Many a times we wonder why this explicit keyword is used when ever we came across any c++ code.
Here I would like to give a simple explanation of this keyword which can be more convincing to all.

Suppose you have a class String

class String {
public:
String (int n);//allocate n bytes to the String object
String(const char *p); // initializes object with char *p
}

Now if you try

String mystring='x';

The char 'x' will be converted to int and will call String(int) constructor. But this is not what the user might have intended. So to prevent such conditions, we can define the class's constructor as explicit.

class String {
public:
explicit String (int n); //allocate n bytes
String(const char *p); // initialize sobject with string p
}

Creating a hash in perl with all the regex matches

As i explained here :http://theunixshell.blogspot.com/2013/01/capturing-all-regex-matches-into-array.html about storing each and each regex match in an array,Now let's see how do we store each and every regex match in a hash instead.

Lets say we have text file like below:

hello world 10 20
world 10 10 10 10 hello 20
hello 30 20 10 world 10


I want to store each and every decimal integer here in a hash as a key and count of their occurances in a value for that key.

Below is the solution for that:

perl -lne '$a{$_}++for(/(\d+)/g);END{for(keys %a){print "$_.$a{$_}"}}' Your_file

output generated will be:

> perl -lne '$a{$_}++for(/(\d+)/g);END{for(keys %a){print "$_.$a{$_}"}}' temp
30.1
10.7
20.3






Tuesday, January 29, 2013

Deleting empty lines from a file

Some times there are some empty lines which we feel are redundant in the file and want to remove them.Below is the command in unix to do that.

sed -i 's/^$//g' your_file

But there is also another way to do this:

grep . your_file > dest_file

In perl also we can acheive this as below:

perl -pi -e 's/^$//g' your_file

the above mentioned perl and sed solutions will do an inplace replacement in the file

If in case the lines have some spaces then:

perl -pi -e 's/^\s*$//g' your_file

Thursday, January 24, 2013

Creating files based upon line number from multiple files

I have 40,000 data files. Each file contains 1445 lines in single column. Now I need to rearrange the data in different order. The first number from each data file need to be collected and dumped in a new file (lets say abc1.dat). This particular file (abc1.dat) will contain 40,000 numbers.
And the second number from each data file need to be extracted and dumped in a another new file (let's say abc2.dat). This new file also will be containing 40,000 numbers. But only second numbers from each data file. At the end of this operation I should have 1445 files (abc1.dat, abc2.dat,...abc40000.dat) and each contains 40,000 data.

Below is a simple way to do it in awk:

awk '{print $1>"abc"FNR".txt"}' file*

file* above should refer to all 40k files.
some flavours of unix does not support the above syntax,in such case go for:

awk '{x="abc"FNR".txt";print $1>x}' file*

Monday, January 21, 2013

List of columns in a table from SYBASE

I recently came across a need to create a file which has the columns of a table in sybase.Then I thought it would be better that we write a script which will fetch the column details by taking table name as an argument and store the output in a text file.Below is a simple perl script to do it.

As you can see help text is also there.So i guess i don't need to explain it.

#!/usr/bin/perl
######################################
#This is the script to list all the
#columns in the table.table names
#should be given as arguments to the
#script.
#example:
#script_name table_name_1 table_name_2 ...so on
######################################

use strict;
use warnings;
my $result;
unlink("output.txt");
foreach(@ARGV)
{
$result = qx{isql -U<user_name> -P<password> -D<dbname> <<EOF
set nocount on
SELECT sc.name FROM syscolumns sc INNER JOIN sysobjects so ON sc.id = so.id WHERE so.name = '$_'
go
exit
EOF
};
my @lines = split /\s+\n/, $result;
splice @lines,0,2;
$_=~s/ //g foreach @lines;

my $outfile  = "output.txt";
open (OUTFILE, ">>$outfile") || die "ERROR: opening $outfile\n";
print OUTFILE "$_\n------------\n".join "\n",@lines;
print OUTFILE "\n\n";
}
close OUTFILE;
################################################################################ 

Thursday, January 17, 2013

Executing a shell command in c/c++ and getting its output

Often we need to execute some shell command and get the output of that shell command in c.
system will simply execute the shell command but ther is no way using it for fetching its output.
below is the way where we can execute the command and also get its output into a c/c++ buffer.


FILE* pipe = popen("your shell command here", "r");
if (!pipe)
{

cerr<<"popen error"<<endl;
}   
char buffer[128];
    while(!feof(pipe))
     {
      if(fgets(buffer, 128, pipe) != NULL){}
     }
pclose(pipe);
buffer[strlen(buffer)-1] = '\0';

Search a string in multiple files including the files in sub directories recursively

Almost every unix programmer will need this at least once in a day.
For searching a string abc in all the files in the current direcory we use:

grep 'abc' *

If you want to serach in files which are under any sub directories also including the files in the current directory then we have to combine both find and grep:

find . -type f|xargs grep 'abc'

Another possible way is using exec of find command:

find . -type f -exec grep 'abc' {} \;

Wednesday, January 16, 2013

Capturing all the regex matches into an array in perl

Often we need to track the list of matches whenever we use a regex.
for example lets say i have a file which looks like below:

Jan 1 1982
Dec 20 1983
jan 6 1984

Now if i want to store all the decimal number in the file in a array..How?

first thing is we need to use a regex match and store all the numbers into an array.

Below is the code for it :

perl -lne 'push @a,/\d+/g;END{print "@a"}' your_file

The above will output:

> perl -lne 'push @a,/\d+/g;END{print "@a"}' your_file
1 1982 20 1983 6 1984


In the same way you can use any other pattern in the match regex and capture those patterns in an array.

Thursday, January 10, 2013

perl's equivalent of "grep -f"

Lets say there are two files:

> cat file1
693661
11002
10995
48981
79600
> cat file2
10993   item    0
11002   item    6
10995   item    7
79600   item    7
439481  item    5
272557  item    7
224325  item    7
84156   item    6
572546  item    7
693661  item    7

using
grep -f file1 file2
we can filter the data in file2 but comparing the matching rows only with teh first column.

I have written a perl equivalent to this but yet not in a complete way.
the below command will simply compare the first field of the file 1 and present the output if it matches with the first field of file2.

> perl -F -lane 'BEGIN{$c=1}if($c==1){$x{$_}++};if(eof && $c==1){$c++;next};if($c==2){if($x{$F[0]}){print }}' file1 file2
11002   item    6
10995   item    7
79600   item    7
693661  item    7

Difference between Java and c++

  1. C++ supports pointers whereas Java does not support pointers. But when many programmers questioned how you can work without pointers, the promoters began saying "Restricted pointers.” So we can say java supports Restricted pointers.
  2. At compilation time Java Source code converts into byte code .The interpreter execute this byte code at run time and gives output .Java is interpreted for the most part and hence platform independent. C++ run and compile using compiler which converts source code into machine level languages so c++ is plate from dependents
  3. Java is platform independent language but c++ is depends upon operating system machine etc. C++ source can be platform independent (and can work on a lot more, especially embedeed, platforms), although the generated objects are generally platofrom dependent but there is clang for llvm which doesn't have this restriction.
  4. Java uses compiler and interpreter both and in c++ their is only compiler
  5. C++ supports operator overloading multiple inheritance but java does not.
  6. C++ is more nearer to hardware then Java
  7. Everything (except fundamental types) is an object in Java (Single root hierarchy as everything gets derived from java.lang.Object).
  8. Java does is a similar to C++ but not have all the complicated aspects of C++ (ex: Pointers, templates, unions, operator overloading, structures etc..) Java does not support conditional compile (#ifdef/#ifndef type).
  9. Thread support is built-in Java but not in C++. C++11, the most recent iteration of the C++ programming language does have Thread support though.
  10. Internet support is built-in Java but not in C++. However c++ has support for socket programming which can be used.
  11. Java does not support header file, include library files just like C++ .Java use import to include different Classes and methods.
  12. Java does not support default arguments like C++.
  13. There is no scope resolution operator :: in Java. It has . using which we can qualify classes with the namespace they came from.
  14. There is no goto statement in Java.
  15. Exception and Auto Garbage Collector handling in Java is different because there are no destructors into Java.
  16. Java has method overloading, but no operator overloading just like c++.
  17. The String class does use the + and += operators to concatenate strings and String expressions use automatic type conversion,
  18. Java is pass-by-value.
  19. Java does not support unsigned integer.

Wednesday, January 9, 2013

Bulk renaming of files in unix

Most of the time it is required that w eneed to rename the files in bulk.
this can be done in many ways,Mostly people do it by  writing a simple shell script.

I found a better way to do it using just the command line in a single command.
Let's say we have some files as shown below.Now i want remove the part -(ab...) from those files.
> ls -1 foo*
foo-bar-(ab-4529111094).txt
foo-bar-foo-bar-(ab-189534).txt
foo-bar-foo-bar-bar-(ab-24937932201).txt
So the expected file names would be :
> ls -1 foo*
foo-bar-foo-bar-bar.txt
foo-bar-foo-bar.txt
foo-bar.txt
>
Below is a simple way to do it.
> ls -1 | nawk '/foo-bar-/{old=$0;gsub(/-\(.*\)/,"",$0);system("mv \""old"\" "$0)}'
Explanation:

ls -1
Will list all the files in a single column in a directory

nawk '/foo-bar-/
The processing is done only for a file names with has foo-bar- as a part of thier names.

old=$0
  initially storing the file name in a variable.
gsub(/-\(.*\)/,"",$0)
 Removing the undesired part of the file name.

mv \""old"\" "$0
This will be expanded to :mv "foo-bar-(ab-4529111094).txt" foo-bar-foo-bar-bar.txt.You might ask why a '"'.because there is possibility that the fine name might consist a space or any other special character(in our case it has '(' ).

system("mv \""old"\" "$0)
This will execute on teh command line what ever is there inside the system call.

Note: nawk is specific for solaris unix.for other falvours of unix just awk is enough.