Thursday, August 11, 2011

find command to exclude binary files


In order to exclude binary files in Linux use the file command 


find . -type f | xargs file | grep -i  ASCII |cut -d: -f1


Explanation:


file command in this case executes the each of the file returned by the find command 
grep ASCII returns only the file having content in the ASCII format
"cut -d: f1"   returns the file name .







Tuesday, August 9, 2011

Perl Program of the day August 10th 2010


See this Place http://linux-forum-karthik.blogspot.com/ everyday to learn a perl program with explanation line by line


Today we see how to substitute a word in the entire file.

Content of List.txt before execution:
Hi gopal,how are you
Hi james,how are you
Hi,how are you
Hi Thomas,how are you
Hi Manjula,how are you
Hi karthik,how are you

Content of the Perl file:

1)#!/usr/bin/perl
2)use File::Copy;
3)open(FH," ;
5)close(FH);
#For creating the temp file and adding the line no
6)$dd=grep(s/Hi/Hai/,@ddd);
7)open(FH1,">list.txt.bak");
8)foreach(@ddd)
9){
10)print FH1;
11)}
12)close(FH1);
13)move("list.txt.bak","list.txt") or die "cannot move";
14)# for moving the temp file to the original files
15)unlink("list.txt.bak") or die "cannot delete the file";
16)# for removing the temp file

Content of List.txt after execution:
Hai gopal,how are you
Hai james,how are you
Hai,how are you
Hai Thomas,how are you
Hai Manjula,how are you
Hai karthik,how are you


Explanation:
1st line:First line starting with "#!" is important as it says the path of the interpreter it depends on the way the perl was installed on your computer.It is very necesary if you don't end the file name with ".pl"
2nd line: use File::Copy is a default module used in Perl so as to use functions Perl functions move and unlink later.
3nd line:opens the file list.txt for reading indicated by the option "<" .FH is the file handler. 4nd line:Entire content of the file list.txt signified by is copied to array @ddd each element in the array @ddd is a line in the file separated by "\n";
5nd line:closing the file handler.
6nd line:This is a important line which does the substitution.
$dd=grep(s/Hi/Hai/,@ddd);for more details regarding grep refer to the Perl article "grep in Perl" with the url http://linux-forum-karthik.blogspot.com/2011/07/grep-in-perl.html
Here grep substitutes Hi with Hai for the entire iteration of the array @ddd.And the no of substitution are saved in the scalar $dd.And the array @ddd is changed now.
7nd line:Here i am basically trying to create a temp file list.txt.bak for writing signified with ">" and the write the content of the array @ddd to it by the line "print FH1".TO understand better refer to the File examples to the right.
13nd line:I am trying to use the move function to move file "list.txt.bak" to "list.txt".
15nd line : i am trying to remove the temp file "list.txt.bak"

Monday, August 8, 2011

Perl Program of the Day August 8th


See this Place http://linux-forum-karthik.blogspot.com/ everyday to learn a perl program with explanation line by line

Today we will learn how to remember a Pattern while substitution.

I will try to explain this concept using a simple program.

Consider i have a line with content "My name is RAMU".Here i want to remember the last word in the line.i.e name of the person and Ask him a question in the format
(Name of the person) ,How are you?


1)#!/usr/bin/perl
2)$line="My name is RAMU";
3)$line =~ s/.*?\b(\w*)$/\1,How are you?/;
4)print $line."\n";

Output:
Ramu,How are you?

Explanation:
1st line:First line starting with "#!" is important as it says the path of the interpreter it depends on the way the perl was installed on your computer.It is very necesary if you don't end the file name with ".pl"
2nd line:$line is a scalar as it starts with "$".So i am trying to store the line in the scalar.

3nd line:This is the line where the main logic happens.
s/.*?\b(\w*)$/\1,How are you?/;
Here:
s  - signifies substitution
".*" -  matches the pattern "My name is " and ? is to make the match non-greedy
\b(\w*)$ 
\b - to specify to start a word-boundary .i.e the name Ramu in this case
\w - typically matches a character in perl since i have specified \w*$ it matches the entire word till the end of the word.
Here the remembrance takes place by the brackets "(  )" any thing inside the brackets are remembered.And the remembered word is stored in \1 in this case.If you made it to remember two patterns the pattern would have been matched into \ 1 and \2 respectively.And this goes on .One  important thing is the brackets should not be backslashed if so the pattern will not be remembered and it will be considered as normal brackets.
/\1,How are you/
As explained above \1 gets replaced by Ramu in this case and Ramu,How are you gets saved in the $line.
4th line:It is simply for the $line scalar to get printed.


If you want to get a basic idea of substitution in Perl refer to yesterday's Program of the day:
Perl program of the day 7th august

Sunday, August 7, 2011

Perl Program of the Day August 7th 2010

See this Place http://linux-forum-karthik.blogspot.com/ everyday to learn a perl program with explanation line by line

Today we will learn to write a simple program to substitute a word with another word in Perl

Here i am trying to sustitute the word "hello" with "Hai" in a line.

1)#!/usr/bin/perl
2)$line="hello great wonderful awesome";
3)$line =~ s/hello/hai/;
4)print $line;

Explanation:
1st line:First line starting with "#!" is important as it says the path of the interpreter it depends on the way the perl was installed on your computer.It is very necesary if you don't end the file name with ".pl"
2nd line:$line is a scalar as it starts with "$".So i am trying to store the line in the
scalar.
3nd line:Here is the main part of the program.
when you want to substitute anything in a scalar.You have to specify that scalar to the left side.Here i want to substitute in the scalar $line so i have specified it to the left.
Next part is "=~" which has to be used when you match or substitute in Perl.
s/hello/hai/
Here "s" specifies substitution
And the word to be replaced has to be given first followed by word to be replaced separated by "/".
By this line the word hello gets replaced by hai.
4nd line:It is just simply to print the line.

Hope this gives a glance of one of the powerful abilities of Perl.

Friday, August 5, 2011

copy and move in perl



use File::Copy;
copy("file1","file2") or die "Copy failed: $!";
copy("Copy.pm",\*STDOUT);
move("/dev1/fileA","/dev2/fileB");




This was refered from http://perldoc.perl.org/File/Copy.html

Thursday, August 4, 2011

Create a Unique list using perl

Consider the following example:


@a=('one','Two','Two','Four','Three','Four','Four','Five');
%seen = (); @uniqu = grep { ! $seen{$_} ++ } @a;
print "@uniqu";

Ouput:

G:\perl_programs>perl perl_grep.pl
one Two Four Three Five

Analysis:
In this case the grep returns $_ when the value is 1.Here since "!" option is used
it reverses the condition so it is 1 when the $seen{$_} value is 0 which happens when the
item comes the first time.

So it will be much more clearer when you use map in place of grep which returns the return value.


@a=('one','Two','Two','Four','Three','Four','Four','Five');
%seen = (); @uniqu = map{ ! $seen{$_} ++ } @a;
print "@uniqu";

Output:

G:\perl_programs>perl perl_grep.pl
1 1 1 1 1
G:\perl_programs>
SO as you may see 1 is returned only for unique values only when it comes first time.

To find the no of lines in the file perl

To find the no of lines in a file a one line command in Perl is sufficient

perl -ne "$. -- if /$^/;END {print $.}" filename

will display the no of lines in the file excluding the blank lines .

Analysis:
"$."  means the current record of the file

"$. --  if /$^/   means the one count will be reduced when it comes across the blank line.

END {print $.} means to print the "$." once it is completed with the file.